home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Modules / cPickle.c < prev    next >
C/C++ Source or Header  |  1999-04-25  |  104KB  |  4,339 lines

  1. /*
  2.  * cPickle.c,v 1.63 1999/02/05 01:40:06 jim Exp
  3.  * 
  4.  * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.  
  5.  * All rights reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions are
  9.  * met:
  10.  * 
  11.  *   o Redistributions of source code must retain the above copyright
  12.  *     notice, this list of conditions, and the disclaimer that follows.
  13.  * 
  14.  *   o Redistributions in binary form must reproduce the above copyright
  15.  *     notice, this list of conditions, and the following disclaimer in
  16.  *     the documentation and/or other materials provided with the
  17.  *     distribution.
  18.  * 
  19.  *   o Neither the name of Digital Creations nor the names of its
  20.  *     contributors may be used to endorse or promote products derived
  21.  *     from this software without specific prior written permission.
  22.  * 
  23.  * 
  24.  * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
  25.  * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  26.  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  27.  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL
  28.  * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31.  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  32.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  33.  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  35.  * DAMAGE.
  36.  * 
  37.  # 
  38.  # If you have questions regarding this software, contact:
  39.  #
  40.  #   Digital Creations, L.C.
  41.  #   910 Princess Ann Street
  42.  #   Fredericksburge, Virginia  22401
  43.  #
  44.  #   info@digicool.com
  45.  #
  46.  #   (540) 371-6909
  47.  */
  48.  
  49. static char cPickle_module_documentation[] = 
  50. "C implementation and optimization of the Python pickle module\n"
  51. "\n"
  52. "cPickle.c,v 1.63 1999/02/05 01:40:06 jim Exp\n"
  53. ;
  54.  
  55. #include "Python.h"
  56. #include "cStringIO.h"
  57. #include "mymath.h"
  58.  
  59. #ifndef Py_eval_input
  60. #include <graminit.h>
  61. #define Py_eval_input eval_input
  62. #endif /* Py_eval_input */
  63.  
  64. #include <errno.h>
  65.  
  66. #include "protos/cPickle.h"
  67.  
  68. #define UNLESS(E) if (!(E))
  69.  
  70. #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
  71.  
  72. #define WRITE_BUF_SIZE 256
  73.  
  74.  
  75. #define MARK        '('
  76. #define STOP        '.'
  77. #define POP         '0'
  78. #define POP_MARK    '1'
  79. #define DUP         '2'
  80. #define FLOAT       'F'
  81. #define BINFLOAT    'G'
  82. #define INT         'I'
  83. #define BININT      'J'
  84. #define BININT1     'K'
  85. #define LONG        'L'
  86. #define BININT2     'M'
  87. #define NONE        'N'
  88. #define PERSID      'P'
  89. #define BINPERSID   'Q'
  90. #define REDUCE      'R'
  91. #define STRING      'S'
  92. #define BINSTRING   'T'
  93. #define SHORT_BINSTRING 'U'
  94. #define APPEND      'a'
  95. #define BUILD       'b'
  96. #define GLOBAL      'c'
  97. #define DICT        'd'
  98. #define EMPTY_DICT  '}'
  99. #define APPENDS     'e'
  100. #define GET         'g'
  101. #define BINGET      'h'
  102. #define INST        'i'
  103. #define LONG_BINGET 'j'
  104. #define LIST        'l'
  105. #define EMPTY_LIST  ']'
  106. #define OBJ         'o'
  107. #define PUT         'p'
  108. #define BINPUT      'q'
  109. #define LONG_BINPUT 'r'
  110. #define SETITEM     's'
  111. #define TUPLE       't'
  112. #define EMPTY_TUPLE ')'
  113. #define SETITEMS    'u'
  114.  
  115. static char MARKv = MARK;
  116.  
  117. /* atol function from string module */
  118. static PyObject *atol_func;
  119.  
  120. static PyObject *PicklingError;
  121. static PyObject *UnpicklingError;
  122. static PyObject *BadPickleGet;
  123.  
  124.  
  125. static PyObject *dispatch_table;
  126. static PyObject *safe_constructors;
  127. static PyObject *empty_tuple;
  128.  
  129. static PyObject *__class___str, *__getinitargs___str, *__dict___str,
  130.   *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
  131.   *write_str, *__safe_for_unpickling___str, *append_str,
  132.   *read_str, *readline_str, *__main___str, *__basicnew___str,
  133.   *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
  134.  
  135. #ifndef PyList_SET_ITEM
  136. #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
  137. #endif
  138. #ifndef PyList_GET_SIZE
  139. #define PyList_GET_SIZE(op)    (((PyListObject *)(op))->ob_size)
  140. #endif
  141. #ifndef PyTuple_SET_ITEM
  142. #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
  143. #endif
  144. #ifndef PyTuple_GET_SIZE
  145. #define PyTuple_GET_SIZE(op)    (((PyTupleObject *)(op))->ob_size)
  146. #endif
  147. #ifndef PyString_GET_SIZE
  148. #define PyString_GET_SIZE(op)    (((PyStringObject *)(op))->ob_size)
  149. #endif
  150.  
  151. /*************************************************************************
  152.  Internal Data type for pickle data.                                     */
  153.  
  154. typedef struct {
  155.      PyObject_HEAD
  156.      int length, size;
  157.      PyObject **data;
  158. } Pdata;
  159.  
  160. static void 
  161. Pdata_dealloc(Pdata *self) {
  162.     int i;
  163.     PyObject **p;
  164.  
  165.     for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
  166.  
  167.     if (self->data) free(self->data);
  168.  
  169.     PyMem_DEL(self);
  170. }
  171.  
  172. static PyTypeObject PdataType = {
  173.     PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
  174.     (destructor)Pdata_dealloc,
  175.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
  176. };
  177.  
  178. #define Pdata_Check(O) ((O)->ob_type == &PdataType)
  179.  
  180. static PyObject *
  181. Pdata_New() {
  182.     Pdata *self;
  183.  
  184.     UNLESS (self = PyObject_NEW(Pdata, &PdataType)) return NULL;
  185.     self->size=8;
  186.     self->length=0;
  187.     self->data=malloc(self->size * sizeof(PyObject*));
  188.     if (self->data) return (PyObject*)self;
  189.     Py_DECREF(self);
  190.     return PyErr_NoMemory();
  191. }
  192.  
  193. static int 
  194. stackUnderflow() {
  195.     PyErr_SetString(UnpicklingError, "unpickling stack underflow");
  196.     return -1;
  197. }
  198.  
  199. static int
  200. Pdata_clear(Pdata *self, int clearto) {
  201.     int i;
  202.     PyObject **p;
  203.  
  204.     if (clearto < 0) return stackUnderflow();
  205.     if (clearto >= self->length) return 0;
  206.  
  207.     for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
  208.       Py_DECREF(*p);
  209.     self->length=clearto;
  210.  
  211.     return 0;
  212. }
  213.  
  214.  
  215. static int 
  216. Pdata_grow(Pdata *self) {
  217.   if (! self->size) {
  218.       PyErr_NoMemory();
  219.       return -1;
  220.   }
  221.   self->size *= 2;                          
  222.   self->data = realloc(self->data, self->size*sizeof(PyObject*));
  223.   if (! self->data) {
  224.     self->size = 0;                       
  225.     PyErr_NoMemory();                              
  226.     return -1;                                     
  227.   }
  228.   return 0;
  229. }                                                      
  230.  
  231. #define PDATA_POP(D,V) {                                      \
  232.     if ((D)->length) V=D->data[--((D)->length)];              \
  233.     else {                                                    \
  234.         PyErr_SetString(UnpicklingError, "bad pickle data");  \
  235.         V=NULL;                                               \
  236.     }                                                         \
  237. }
  238.  
  239.  
  240. static PyObject *
  241. Pdata_popTuple(Pdata *self, int start) {
  242.     PyObject *r;
  243.     int i, j, l;
  244.  
  245.     l=self->length-start;
  246.     UNLESS (r=PyTuple_New(l)) return NULL;
  247.     for (i=start, j=0 ; j < l; )
  248.         PyTuple_SET_ITEM(r,j++,self->data[i++]);
  249.  
  250.     self->length=start;
  251.     return r;
  252. }
  253.  
  254. static PyObject *
  255. Pdata_popList(Pdata *self, int start) {
  256.     PyObject *r;
  257.     int i, j, l;
  258.  
  259.     l=self->length-start;
  260.     UNLESS (r=PyList_New(l)) return NULL;
  261.     for (i=start, j=0 ; j < l; )
  262.         PyList_SET_ITEM(r,j++,self->data[i++]);
  263.  
  264.     self->length=start;
  265.     return r;
  266. }
  267.  
  268. #define PDATA_APPEND_(D,O,ER) { \
  269.   if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
  270. }
  271.  
  272. #define PDATA_APPEND(D,O,ER) {                                 \
  273.     if (((Pdata*)(D))->length == ((Pdata*)(D))->size &&        \
  274.         Pdata_grow((Pdata*)(D)) < 0)                           \
  275.         return ER;                                             \
  276.     Py_INCREF(O);                                              \
  277.     ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O;            \
  278. }
  279.  
  280. #define PDATA_PUSH(D,O,ER) {                                   \
  281.     if (((Pdata*)(D))->length == ((Pdata*)(D))->size &&        \
  282.         Pdata_grow((Pdata*)(D)) < 0) {                         \
  283.         Py_DECREF(O);                                          \
  284.         return ER;                                             \
  285.     }                                                          \
  286.     ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O;            \
  287. }
  288.  
  289. /*************************************************************************/
  290.  
  291. #define ARG_TUP(self, o) {                          \
  292.   if (self->arg || (self->arg=PyTuple_New(1))) {    \
  293.       Py_XDECREF(PyTuple_GET_ITEM(self->arg,0));    \
  294.       PyTuple_SET_ITEM(self->arg,0,o);              \
  295.   }                                                 \
  296.   else {                                            \
  297.       Py_DECREF(o);                                 \
  298.   }                                                 \
  299. }
  300.  
  301. #define FREE_ARG_TUP(self) {                        \
  302.     if (self->arg->ob_refcnt > 1) {                 \
  303.       Py_DECREF(self->arg);                         \
  304.       self->arg=NULL;                               \
  305.     }                                               \
  306.   }
  307.  
  308. typedef struct _xxx_picklerobj_struct {
  309.      PyObject_HEAD
  310.      FILE *fp;
  311.      PyObject *write;
  312.      PyObject *file;
  313.      PyObject *memo;
  314.      PyObject *arg;
  315.      PyObject *pers_func;
  316.      PyObject *inst_pers_func;
  317.      int bin;
  318.      int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
  319.      int (*write_func) Py_PROTO((struct _xxx_picklerobj_struct *self, char *s, int  n));
  320.      char *write_buf;
  321.      int buf_size;
  322.      PyObject *dispatch_table;
  323. } Picklerobject;
  324.  
  325. static int save Py_PROTO((Picklerobject *self, PyObject *args, int  pers_save));
  326. static int put2 Py_PROTO((Picklerobject *self, PyObject *ob));
  327.  
  328. staticforward PyTypeObject Picklertype;
  329.  
  330. typedef struct _xxx_unpicklerobj_struct {
  331.      PyObject_HEAD
  332.      FILE *fp;
  333.      PyObject *file;
  334.      PyObject *readline;
  335.      PyObject *read;
  336.      PyObject *memo;
  337.      PyObject *arg;
  338.      Pdata *stack;
  339.      PyObject *mark;
  340.      PyObject *pers_func;
  341.      PyObject *last_string;
  342.      int *marks;
  343.      int num_marks;
  344.      int marks_size;
  345.      int (*read_func) Py_PROTO((struct _xxx_unpicklerobj_struct *self, char **s, int  n));
  346.      int (*readline_func) Py_PROTO((struct _xxx_unpicklerobj_struct *self, char **s));
  347.      int buf_size;
  348.      char *buf;
  349.      PyObject *safe_constructors;
  350. } Unpicklerobject;
  351.  
  352. staticforward PyTypeObject Unpicklertype;
  353.  
  354. int 
  355. cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
  356.     PyObject *v;
  357.  
  358.     if ((v = PyObject_GetItem(o,key))) {
  359.         Py_DECREF(v);
  360.         return 1;
  361.     }
  362.  
  363.     PyErr_Clear();
  364.     return 0;
  365. }
  366.  
  367. static
  368. PyObject *
  369. #ifdef HAVE_STDARG_PROTOTYPES
  370. /* VARARGS 2 */
  371. cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
  372. #else
  373. /* VARARGS */
  374. cPickle_ErrFormat(va_alist) va_dcl {
  375. #endif
  376.   va_list va;
  377.   PyObject *args=0, *retval=0;
  378. #ifdef HAVE_STDARG_PROTOTYPES
  379.   va_start(va, format);
  380. #else
  381.   PyObject *ErrType;
  382.   char *stringformat, *format;
  383.   va_start(va);
  384.   ErrType = va_arg(va, PyObject *);
  385.   stringformat   = va_arg(va, char *);
  386.   format   = va_arg(va, char *);
  387. #endif
  388.   
  389.   if (format) args = Py_VaBuildValue(format, va);
  390.   va_end(va);
  391.   if (format && ! args) return NULL;
  392.   if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
  393.  
  394.   if (retval) {
  395.       if (args) {
  396.           PyObject *v;
  397.           v=PyString_Format(retval, args);
  398.           Py_DECREF(retval);
  399.           Py_DECREF(args);
  400.           if (! v) return NULL;
  401.           retval=v;
  402.         }
  403.     }
  404.   else
  405.     if (args) retval=args;
  406.     else {
  407.         PyErr_SetObject(ErrType,Py_None);
  408.         return NULL;
  409.       }
  410.   PyErr_SetObject(ErrType,retval);
  411.   Py_DECREF(retval);
  412.   return NULL;
  413. }
  414.  
  415. static int 
  416. write_file(Picklerobject *self, char *s, int  n) {
  417.     if (s == NULL) {
  418.         return 0;
  419.     }
  420.  
  421.     if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
  422.         PyErr_SetFromErrno(PyExc_IOError);
  423.         return -1;
  424.     }
  425.  
  426.     return n;
  427. }
  428.  
  429. static int 
  430. write_cStringIO(Picklerobject *self, char *s, int  n) {
  431.     if (s == NULL) {
  432.         return 0;
  433.     }
  434.  
  435.     if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
  436.         return -1;
  437.     }
  438.  
  439.     return n;
  440. }
  441.  
  442. static int 
  443. write_none(Picklerobject *self, char *s, int  n) {
  444.     if (s == NULL) return 0;
  445.     return n;
  446. }
  447.  
  448. static int 
  449. write_other(Picklerobject *self, char *s, int  n) {
  450.     PyObject *py_str = 0, *junk = 0;
  451.  
  452.     if (s == NULL) {
  453.         UNLESS (self->buf_size) return 0;
  454.         UNLESS (py_str = 
  455.             PyString_FromStringAndSize(self->write_buf, self->buf_size))
  456.             return -1;
  457.     }
  458.     else {
  459.         if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
  460.             if (write_other(self, NULL, 0) < 0)
  461.                 return -1;
  462.         }
  463.  
  464.         if (n > WRITE_BUF_SIZE) {    
  465.             UNLESS (py_str = 
  466.                 PyString_FromStringAndSize(s, n))
  467.                 return -1;
  468.         }
  469.         else {
  470.             memcpy(self->write_buf + self->buf_size, s, n);
  471.             self->buf_size += n;
  472.             return n;
  473.         }
  474.     }
  475.  
  476.     if (self->write) {
  477.         /* object with write method */
  478.         ARG_TUP(self, py_str);
  479.         if (self->arg) {
  480.             junk = PyObject_CallObject(self->write, self->arg);
  481.             FREE_ARG_TUP(self);
  482.         }
  483.         if (junk) Py_DECREF(junk);
  484.         else return -1;
  485.       }
  486.     else 
  487.       PDATA_PUSH(self->file, py_str, -1);
  488.     
  489.     self->buf_size = 0; 
  490.     return n;
  491. }
  492.  
  493.  
  494. static int 
  495. read_file(Unpicklerobject *self, char **s, int  n) {
  496.  
  497.     if (self->buf_size == 0) {
  498.         int size;
  499.  
  500.         size = ((n < 32) ? 32 : n); 
  501.         UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
  502.             PyErr_NoMemory();
  503.             return -1;
  504.         }
  505.  
  506.         self->buf_size = size;
  507.     }
  508.     else if (n > self->buf_size) {
  509.         UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
  510.             PyErr_NoMemory();
  511.             return -1;
  512.         }
  513.  
  514.         self->buf_size = n;
  515.     }
  516.             
  517.     if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {  
  518.         if (feof(self->fp)) {
  519.             PyErr_SetNone(PyExc_EOFError);
  520.             return -1;
  521.         }
  522.  
  523.         PyErr_SetFromErrno(PyExc_IOError);
  524.         return -1;
  525.     }
  526.  
  527.     *s = self->buf;
  528.  
  529.     return n;
  530. }
  531.  
  532.  
  533. static int 
  534. readline_file(Unpicklerobject *self, char **s) {
  535.     int i;
  536.  
  537.     if (self->buf_size == 0) {
  538.         UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
  539.             PyErr_NoMemory();
  540.             return -1;
  541.         }
  542.    
  543.         self->buf_size = 40;
  544.     }
  545.  
  546.     i = 0;
  547.     while (1) {
  548.         for (; i < (self->buf_size - 1); i++) {
  549.             if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
  550.                 self->buf[i + 1] = '\0';
  551.                 *s = self->buf;
  552.                 return i + 1;
  553.             }
  554.         }
  555.  
  556.         UNLESS (self->buf = (char *)realloc(self->buf, 
  557.             (self->buf_size * 2) * sizeof(char))) {
  558.             PyErr_NoMemory();
  559.             return -1;
  560.         }
  561.  
  562.         self->buf_size *= 2;
  563.     }
  564.  
  565. }    
  566.  
  567.  
  568. static int 
  569. read_cStringIO(Unpicklerobject *self, char **s, int  n) {
  570.     char *ptr;
  571.  
  572.     if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
  573.         PyErr_SetNone(PyExc_EOFError);
  574.         return -1;
  575.     }
  576.  
  577.     *s = ptr;
  578.  
  579.     return n;
  580. }
  581.  
  582.  
  583. static int 
  584. readline_cStringIO(Unpicklerobject *self, char **s) {
  585.     int n;
  586.     char *ptr;
  587.  
  588.     if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
  589.         return -1;
  590.     }
  591.  
  592.     *s = ptr;
  593.  
  594.     return n;
  595. }
  596.  
  597.  
  598. static int 
  599. read_other(Unpicklerobject *self, char **s, int  n) {
  600.     PyObject *bytes, *str=0;
  601.     int res = -1;
  602.  
  603.     UNLESS (bytes = PyInt_FromLong(n)) return -1;
  604.  
  605.     ARG_TUP(self, bytes);
  606.     if (self->arg) {
  607.         str = PyObject_CallObject(self->read, self->arg);
  608.         FREE_ARG_TUP(self);
  609.     }
  610.     if (! str) return -1;
  611.  
  612.     Py_XDECREF(self->last_string);
  613.     self->last_string = str;
  614.  
  615.     if (! (*s = PyString_AsString(str))) return -1;
  616.     return n;
  617. }
  618.  
  619.  
  620. static int 
  621. readline_other(Unpicklerobject *self, char **s) {
  622.     PyObject *str;
  623.     int str_size;
  624.  
  625.     UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
  626.         return -1;
  627.     }
  628.  
  629.     if ((str_size = PyString_Size(str)) < 0)
  630.       return -1;
  631.  
  632.     Py_XDECREF(self->last_string);
  633.     self->last_string = str;
  634.  
  635.     if (! (*s = PyString_AsString(str)))
  636.       return -1;
  637.  
  638.     return str_size;
  639. }
  640.  
  641.  
  642. static char *
  643. pystrndup(char *s, int l) {
  644.   char *r;
  645.   UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
  646.   memcpy(r,s,l);
  647.   r[l]=0;
  648.   return r;
  649. }
  650.  
  651.  
  652. static int
  653. get(Picklerobject *self, PyObject *id) {
  654.     PyObject *value, *mv;
  655.     long c_value;
  656.     char s[30];
  657.     int len;
  658.  
  659.     UNLESS (mv = PyDict_GetItem(self->memo, id)) {
  660.         PyErr_SetObject(PyExc_KeyError, id);
  661.         return -1;
  662.       }
  663.  
  664.     UNLESS (value = PyTuple_GetItem(mv, 0))
  665.         return -1;
  666.         
  667.     UNLESS (PyInt_Check(value)) {
  668.       PyErr_SetString(PicklingError, "no int where int expected in memo");
  669.       return -1;
  670.     }
  671.     c_value = PyInt_AS_LONG((PyIntObject*)value);
  672.  
  673.     if (!self->bin) {
  674.         s[0] = GET;
  675.         sprintf(s + 1, "%ld\n", c_value);
  676.         len = strlen(s);
  677.     }
  678.     else if (Pdata_Check(self->file)) {
  679.         if (write_other(self, NULL, 0) < 0) return -1;
  680.         PDATA_APPEND(self->file, mv, -1);
  681.         return 0;
  682.       }
  683.     else {
  684.         if (c_value < 256) {
  685.             s[0] = BINGET;
  686.             s[1] = (int)(c_value & 0xff);
  687.             len = 2;
  688.         }
  689.         else {
  690.             s[0] = LONG_BINGET;
  691.             s[1] = (int)(c_value & 0xff);
  692.             s[2] = (int)((c_value >> 8)  & 0xff);
  693.             s[3] = (int)((c_value >> 16) & 0xff);
  694.             s[4] = (int)((c_value >> 24) & 0xff);
  695.             len = 5;
  696.         }
  697.     }
  698.  
  699.     if ((*self->write_func)(self, s, len) < 0)
  700.         return -1;
  701.  
  702.     return 0;
  703. }
  704.     
  705.  
  706. static int
  707. put(Picklerobject *self, PyObject *ob) {
  708.     if (ob->ob_refcnt < 2 || self->fast)
  709.         return 0;
  710.  
  711.     return put2(self, ob);
  712. }
  713.  
  714.   
  715. static int
  716. put2(Picklerobject *self, PyObject *ob) {
  717.     char c_str[30];
  718.     int p, len, res = -1;
  719.     PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
  720.  
  721.     if (self->fast) return 0;
  722.  
  723.     if ((p = PyDict_Size(self->memo)) < 0)
  724.         goto finally;
  725.  
  726.     p++;  /* Make sure memo keys are positive! */
  727.  
  728.     UNLESS (py_ob_id = PyInt_FromLong((long)ob))
  729.         goto finally;
  730.  
  731.     UNLESS (memo_len = PyInt_FromLong(p))
  732.         goto finally;
  733.  
  734.     UNLESS (t = PyTuple_New(2))
  735.         goto finally;
  736.  
  737.     PyTuple_SET_ITEM(t, 0, memo_len);
  738.     Py_INCREF(memo_len);
  739.     PyTuple_SET_ITEM(t, 1, ob);
  740.     Py_INCREF(ob);
  741.  
  742.     if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
  743.         goto finally;
  744.  
  745.     if (!self->bin) {
  746.         c_str[0] = PUT;
  747.         sprintf(c_str + 1, "%d\n", p);
  748.         len = strlen(c_str);
  749.     }
  750.     else if (Pdata_Check(self->file)) {
  751.         if (write_other(self, NULL, 0) < 0) return -1;
  752.         PDATA_APPEND(self->file, memo_len, -1);
  753.         res=0;          /* Job well done ;) */
  754.         goto finally;
  755.     }
  756.     else {
  757.         if (p >= 256) {
  758.             c_str[0] = LONG_BINPUT;
  759.             c_str[1] = (int)(p & 0xff);
  760.             c_str[2] = (int)((p >> 8)  & 0xff);
  761.             c_str[3] = (int)((p >> 16) & 0xff);
  762.             c_str[4] = (int)((p >> 24) & 0xff);
  763.             len = 5;
  764.         }
  765.         else {
  766.             c_str[0] = BINPUT;
  767.             c_str[1] = p;
  768.             len = 2; 
  769.         }
  770.     }
  771.  
  772.     if ((*self->write_func)(self, c_str, len) < 0)
  773.         goto finally;
  774.  
  775.     res = 0;
  776.  
  777. finally:
  778.     Py_XDECREF(py_ob_id);
  779.     Py_XDECREF(memo_len);
  780.     Py_XDECREF(t);
  781.  
  782.     return res;
  783. }
  784.  
  785. #define PyImport_Import cPickle_Import
  786.  
  787. static PyObject *
  788. PyImport_Import(PyObject *module_name) {
  789.   static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
  790.   static PyObject *standard_builtins=0;
  791.   PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
  792.  
  793.   UNLESS (silly_list) {
  794.       UNLESS (__import___str=PyString_FromString("__import__"))
  795.         return NULL;
  796.       UNLESS (__builtins___str=PyString_FromString("__builtins__"))
  797.         return NULL;
  798.       UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
  799.         return NULL;
  800.     }
  801.  
  802.   if ((globals=PyEval_GetGlobals())) {
  803.       Py_INCREF(globals);
  804.       UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
  805.         goto err;
  806.     }
  807.   else {
  808.       PyErr_Clear();
  809.  
  810.       UNLESS (standard_builtins ||
  811.              (standard_builtins=PyImport_ImportModule("__builtin__")))
  812.         return NULL;
  813.       
  814.       __builtins__=standard_builtins;
  815.       Py_INCREF(__builtins__);
  816.       UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
  817.         goto err;
  818.     }
  819.  
  820.   if (PyDict_Check(__builtins__)) {
  821.     UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
  822.   }
  823.   else {
  824.     UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
  825.   }
  826.  
  827.   UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
  828.                                  module_name, globals, globals, silly_list))
  829.     goto err;
  830.  
  831.   Py_DECREF(globals);
  832.   Py_DECREF(__builtins__);
  833.   Py_DECREF(__import__);
  834.   
  835.   return r;
  836. err:
  837.   Py_XDECREF(globals);
  838.   Py_XDECREF(__builtins__);
  839.   Py_XDECREF(__import__);
  840.   return NULL;
  841. }
  842.  
  843. static PyObject *
  844. whichmodule(PyObject *global, PyObject *global_name) {
  845.     int i, j;
  846.     PyObject *module = 0, *modules_dict = 0,
  847.         *global_name_attr = 0, *name = 0;
  848.  
  849.     module = PyObject_GetAttrString(global, "__module__");
  850.     if (module) return module;
  851.     PyErr_Clear();
  852.  
  853.     UNLESS (modules_dict = PySys_GetObject("modules"))
  854.         return NULL;
  855.  
  856.     i = 0;
  857.     while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
  858.  
  859.         if (PyObject_Compare(name, __main___str)==0) continue;
  860.       
  861.         UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
  862.             PyErr_Clear();
  863.             continue;
  864.         }
  865.  
  866.         if (global_name_attr != global) {
  867.             Py_DECREF(global_name_attr);
  868.             continue;
  869.         }
  870.  
  871.         Py_DECREF(global_name_attr);
  872.  
  873.         break;
  874.     }
  875.  
  876.     /* The following implements the rule in pickle.py added in 1.5
  877.        that used __main__ if no module is found.  I don't actually
  878.        like this rule. jlf
  879.      */
  880.     if (!j) {
  881.         j=1;
  882.         name=__main___str;
  883.     }
  884.  
  885.     Py_INCREF(name);
  886.     return name;
  887. }
  888.  
  889.  
  890. static int
  891. save_none(Picklerobject *self, PyObject *args) {
  892.     static char none = NONE;
  893.     if ((*self->write_func)(self, &none, 1) < 0)  
  894.         return -1;
  895.  
  896.     return 0;
  897. }
  898.  
  899.       
  900. static int
  901. save_int(Picklerobject *self, PyObject *args) {
  902.     char c_str[32];
  903.     long l = PyInt_AS_LONG((PyIntObject *)args);
  904.     int len = 0;
  905.  
  906.     if (!self->bin
  907. #if SIZEOF_LONG > 4
  908.         || (l >> 32)
  909. #endif
  910.             ) {
  911.                 /* Save extra-long ints in non-binary mode, so that
  912.                    we can use python long parsing code to restore,
  913.                    if necessary. */
  914.         c_str[0] = INT;
  915.         sprintf(c_str + 1, "%ld\n", l);
  916.         if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
  917.             return -1;
  918.     }
  919.     else {
  920.         c_str[1] = (int)( l        & 0xff);
  921.         c_str[2] = (int)((l >> 8)  & 0xff);
  922.         c_str[3] = (int)((l >> 16) & 0xff);
  923.         c_str[4] = (int)((l >> 24) & 0xff);
  924.  
  925.         if ((c_str[4] == 0) && (c_str[3] == 0)) {
  926.             if (c_str[2] == 0) {
  927.                 c_str[0] = BININT1;
  928.                 len = 2;
  929.             }
  930.             else {
  931.                 c_str[0] = BININT2;
  932.                 len = 3;
  933.             }
  934.         }
  935.         else {
  936.             c_str[0] = BININT;
  937.             len = 5;
  938.         }
  939.  
  940.         if ((*self->write_func)(self, c_str, len) < 0)
  941.             return -1;
  942.     }
  943.  
  944.     return 0;
  945. }
  946.  
  947.  
  948. static int
  949. save_long(Picklerobject *self, PyObject *args) {
  950.     int size, res = -1;
  951.     PyObject *repr = 0;
  952.  
  953.     static char l = LONG;
  954.  
  955.     UNLESS (repr = PyObject_Repr(args))
  956.         goto finally;
  957.  
  958.     if ((size = PyString_Size(repr)) < 0)
  959.         goto finally;
  960.  
  961.     if ((*self->write_func)(self, &l, 1) < 0)
  962.         goto finally;
  963.  
  964.     if ((*self->write_func)(self, 
  965.         PyString_AS_STRING((PyStringObject *)repr), size) < 0)
  966.         goto finally;
  967.  
  968.     if ((*self->write_func)(self, "\n", 1) < 0)
  969.         goto finally;
  970.  
  971.     res = 0;
  972.  
  973. finally:
  974.     Py_XDECREF(repr);
  975.  
  976.     return res;
  977. }
  978.  
  979.  
  980. static int
  981. save_float(Picklerobject *self, PyObject *args) {
  982.     double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
  983.  
  984.     if (self->bin) {
  985.         int s, e;
  986.         double f;
  987.         long fhi, flo;
  988.         char str[9], *p = str;
  989.  
  990.         *p = BINFLOAT;
  991.         p++;
  992.  
  993.         if (x < 0) {
  994.             s = 1;
  995.             x = -x;
  996.         }
  997.         else
  998.             s = 0;
  999.  
  1000.         f = frexp(x, &e);
  1001.  
  1002.         /* Normalize f to be in the range [1.0, 2.0) */
  1003.         if (0.5 <= f && f < 1.0) {
  1004.             f *= 2.0;
  1005.             e--;
  1006.         }
  1007.         else if (f == 0.0) {
  1008.             e = 0;
  1009.         }
  1010.         else {
  1011.             PyErr_SetString(PyExc_SystemError,
  1012.                             "frexp() result out of range");
  1013.             return -1;
  1014.         }
  1015.  
  1016.         if (e >= 1024) {
  1017.             /* XXX 1024 itself is reserved for Inf/NaN */
  1018.             PyErr_SetString(PyExc_OverflowError,
  1019.                             "float too large to pack with d format");
  1020.             return -1;
  1021.         }
  1022.         else if (e < -1022) {
  1023.             /* Gradual underflow */
  1024.             f = ldexp(f, 1022 + e);
  1025.             e = 0;
  1026.         }
  1027.         else if (!(e == 0 && f == 0.0)) {
  1028.             e += 1023;
  1029.             f -= 1.0; /* Get rid of leading 1 */
  1030.         }
  1031.  
  1032.         /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
  1033.         f *= 268435456.0; /* 2**28 */
  1034.         fhi = (long) floor(f); /* Truncate */
  1035.         f -= (double)fhi;
  1036.         f *= 16777216.0; /* 2**24 */
  1037.         flo = (long) floor(f + 0.5); /* Round */
  1038.  
  1039.         /* First byte */
  1040.         *p = (s<<7) | (e>>4);
  1041.         p++;
  1042.  
  1043.         /* Second byte */
  1044.         *p = (char) (((e&0xF)<<4) | (fhi>>24));
  1045.         p++;
  1046.  
  1047.         /* Third byte */
  1048.         *p = (fhi>>16) & 0xFF;
  1049.         p++;
  1050.  
  1051.         /* Fourth byte */
  1052.         *p = (fhi>>8) & 0xFF;
  1053.         p++;
  1054.  
  1055.         /* Fifth byte */
  1056.         *p = fhi & 0xFF;
  1057.         p++;
  1058.  
  1059.         /* Sixth byte */
  1060.         *p = (flo>>16) & 0xFF;
  1061.         p++;
  1062.  
  1063.         /* Seventh byte */
  1064.         *p = (flo>>8) & 0xFF;
  1065.         p++;
  1066.  
  1067.         /* Eighth byte */
  1068.         *p = flo & 0xFF;
  1069.  
  1070.         if ((*self->write_func)(self, str, 9) < 0)
  1071.             return -1;
  1072.     }
  1073.     else {
  1074.         char c_str[250];
  1075.         c_str[0] = FLOAT;
  1076.         sprintf(c_str + 1, "%.17g\n", x);
  1077.  
  1078.         if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
  1079.             return -1;
  1080.     }
  1081.  
  1082.     return 0;
  1083. }
  1084.  
  1085.  
  1086. static int
  1087. save_string(Picklerobject *self, PyObject *args, int doput) {
  1088.     int size, len;
  1089.     PyObject *repr=0;
  1090.  
  1091.     if ((size = PyString_Size(args)) < 0)
  1092.       return -1;
  1093.  
  1094.     if (!self->bin) {
  1095.         char *repr_str;
  1096.  
  1097.         static char string = STRING;
  1098.  
  1099.         UNLESS (repr = PyObject_Repr(args))
  1100.             return -1;
  1101.  
  1102.         if ((len = PyString_Size(repr)) < 0)
  1103.           goto err;
  1104.         repr_str = PyString_AS_STRING((PyStringObject *)repr);
  1105.  
  1106.         if ((*self->write_func)(self, &string, 1) < 0)
  1107.             goto err;
  1108.  
  1109.         if ((*self->write_func)(self, repr_str, len) < 0)
  1110.             goto err;
  1111.  
  1112.         if ((*self->write_func)(self, "\n", 1) < 0)
  1113.             goto err;
  1114.  
  1115.         Py_XDECREF(repr);
  1116.     }
  1117.     else {
  1118.         int i;
  1119.         char c_str[5];
  1120.  
  1121.         if ((size = PyString_Size(args)) < 0)
  1122.           return -1;
  1123.  
  1124.         if (size < 256) {
  1125.             c_str[0] = SHORT_BINSTRING;
  1126.             c_str[1] = size;
  1127.             len = 2;
  1128.         }
  1129.         else {
  1130.             c_str[0] = BINSTRING;
  1131.             for (i = 1; i < 5; i++)
  1132.                 c_str[i] = (int)(size >> ((i - 1) * 8));
  1133.             len = 5;
  1134.         }
  1135.  
  1136.         if ((*self->write_func)(self, c_str, len) < 0)
  1137.             return -1;
  1138.  
  1139.         if (size > 128 && Pdata_Check(self->file)) {
  1140.             if (write_other(self, NULL, 0) < 0) return -1;
  1141.             PDATA_APPEND(self->file, args, -1);
  1142.           }
  1143.         else {
  1144.           if ((*self->write_func)(self, 
  1145.               PyString_AS_STRING((PyStringObject *)args), size) < 0)
  1146.             return -1;
  1147.         }
  1148.     }
  1149.  
  1150.     if (doput)
  1151.       if (put(self, args) < 0)
  1152.         return -1;
  1153.  
  1154.     return 0;
  1155.  
  1156. err:
  1157.     Py_XDECREF(repr);
  1158.     return -1;
  1159. }
  1160.  
  1161.  
  1162. static int
  1163. save_tuple(Picklerobject *self, PyObject *args) {
  1164.     PyObject *element = 0, *py_tuple_id = 0;
  1165.     int len, i, has_key, res = -1;
  1166.  
  1167.     static char tuple = TUPLE;
  1168.  
  1169.     if ((*self->write_func)(self, &MARKv, 1) < 0)
  1170.         goto finally;
  1171.  
  1172.     if ((len = PyTuple_Size(args)) < 0)  
  1173.         goto finally;
  1174.  
  1175.     for (i = 0; i < len; i++) {
  1176.         UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))  
  1177.             goto finally;
  1178.     
  1179.         if (save(self, element, 0) < 0)
  1180.             goto finally;
  1181.     }
  1182.  
  1183.     UNLESS (py_tuple_id = PyInt_FromLong((long)args))
  1184.         goto finally;
  1185.  
  1186.     if (len) {
  1187.         if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
  1188.             goto finally;
  1189.  
  1190.         if (has_key) {
  1191.             if (self->bin) {
  1192.                 static char pop_mark = POP_MARK;
  1193.   
  1194.                 if ((*self->write_func)(self, &pop_mark, 1) < 0)
  1195.                     goto finally;
  1196.             }
  1197.             else {
  1198.                 static char pop = POP;
  1199.        
  1200.                 for (i = 0; i <= len; i++) {
  1201.                     if ((*self->write_func)(self, &pop, 1) < 0)
  1202.                         goto finally;
  1203.                 } 
  1204.             }
  1205.         
  1206.             if (get(self, py_tuple_id) < 0)
  1207.                 goto finally;
  1208.  
  1209.             res = 0;
  1210.             goto finally;
  1211.         }
  1212.     }
  1213.  
  1214.     if ((*self->write_func)(self, &tuple, 1) < 0) {
  1215.         goto finally;
  1216.     }
  1217.  
  1218.     if (put(self, args) < 0)
  1219.         goto finally;
  1220.  
  1221.     res = 0;
  1222.  
  1223. finally:
  1224.     Py_XDECREF(py_tuple_id);
  1225.  
  1226.     return res;
  1227. }
  1228.  
  1229. static int
  1230. save_empty_tuple(Picklerobject *self, PyObject *args) {
  1231.     static char tuple = EMPTY_TUPLE;
  1232.  
  1233.     return (*self->write_func)(self, &tuple, 1);
  1234. }
  1235.  
  1236.  
  1237. static int
  1238. save_list(Picklerobject *self, PyObject *args) {
  1239.     PyObject *element = 0;
  1240.     int s_len, len, i, using_appends, res = -1;
  1241.     char s[3];
  1242.  
  1243.     static char append = APPEND, appends = APPENDS;
  1244.  
  1245.     if (self->bin) {
  1246.         s[0] = EMPTY_LIST;
  1247.         s_len = 1;
  1248.     } 
  1249.     else {
  1250.         s[0] = MARK;
  1251.         s[1] = LIST;
  1252.         s_len = 2;
  1253.     }
  1254.  
  1255.     if ((len = PyList_Size(args)) < 0)
  1256.         goto finally;
  1257.  
  1258.     if ((*self->write_func)(self, s, s_len) < 0)
  1259.         goto finally;
  1260.  
  1261.     if (len == 0) {
  1262.         if (put(self, args) < 0)
  1263.             goto finally;
  1264.     }
  1265.     else {
  1266.         if (put2(self, args) < 0)
  1267.             goto finally;
  1268.     }
  1269.  
  1270.     if ((using_appends = (self->bin && (len > 1))))
  1271.         if ((*self->write_func)(self, &MARKv, 1) < 0)
  1272.             goto finally;
  1273.  
  1274.     for (i = 0; i < len; i++) {
  1275.         UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))  
  1276.             goto finally;
  1277.  
  1278.         if (save(self, element, 0) < 0)  
  1279.             goto finally;       
  1280.  
  1281.         if (!using_appends) {
  1282.             if ((*self->write_func)(self, &append, 1) < 0)
  1283.                 goto finally;
  1284.         }
  1285.     }
  1286.  
  1287.     if (using_appends) {
  1288.         if ((*self->write_func)(self, &appends, 1) < 0)
  1289.             goto finally;
  1290.     }
  1291.  
  1292.     res = 0;
  1293.  
  1294. finally:
  1295.  
  1296.     return res;
  1297. }
  1298.  
  1299.  
  1300. static int
  1301. save_dict(Picklerobject *self, PyObject *args) {
  1302.     PyObject *key = 0, *value = 0;
  1303.     int i, len, res = -1, using_setitems;
  1304.     char s[3];
  1305.  
  1306.     static char setitem = SETITEM, setitems = SETITEMS;
  1307.  
  1308.     if (self->bin) {
  1309.         s[0] = EMPTY_DICT;
  1310.         len = 1;
  1311.     }
  1312.     else {
  1313.         s[0] = MARK;
  1314.         s[1] = DICT;
  1315.         len = 2;
  1316.     }
  1317.  
  1318.     if ((*self->write_func)(self, s, len) < 0)
  1319.         goto finally;
  1320.  
  1321.     if ((len = PyDict_Size(args)) < 0)
  1322.         goto finally;
  1323.  
  1324.     if (len == 0) {
  1325.         if (put(self, args) < 0)
  1326.             goto finally;
  1327.     }
  1328.     else {
  1329.         if (put2(self, args) < 0)
  1330.             goto finally;
  1331.     }
  1332.  
  1333.     if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
  1334.         if ((*self->write_func)(self, &MARKv, 1) < 0)
  1335.             goto finally;
  1336.  
  1337.     i = 0;
  1338.     while (PyDict_Next(args, &i, &key, &value)) {
  1339.         if (save(self, key, 0) < 0)
  1340.             goto finally;
  1341.  
  1342.         if (save(self, value, 0) < 0)
  1343.             goto finally;
  1344.  
  1345.         if (!using_setitems) {
  1346.             if ((*self->write_func)(self, &setitem, 1) < 0)
  1347.                 goto finally;
  1348.         }
  1349.     }
  1350.  
  1351.     if (using_setitems) {
  1352.         if ((*self->write_func)(self, &setitems, 1) < 0)
  1353.             goto finally;
  1354.     }
  1355.  
  1356.     res = 0;
  1357.  
  1358. finally:
  1359.  
  1360.     return res;
  1361. }
  1362.  
  1363.  
  1364. static int  
  1365. save_inst(Picklerobject *self, PyObject *args) {
  1366.     PyObject *class = 0, *module = 0, *name = 0, *state = 0, 
  1367.              *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
  1368.     char *module_str, *name_str;
  1369.     int module_size, name_size, res = -1;
  1370.  
  1371.     static char inst = INST, obj = OBJ, build = BUILD;
  1372.  
  1373.     if ((*self->write_func)(self, &MARKv, 1) < 0)
  1374.         goto finally;
  1375.  
  1376.     UNLESS (class = PyObject_GetAttr(args, __class___str))
  1377.         goto finally;
  1378.  
  1379.     if (self->bin) {
  1380.         if (save(self, class, 0) < 0)
  1381.             goto finally;
  1382.     }
  1383.  
  1384.     if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
  1385.         PyObject *element = 0;
  1386.         int i, len;
  1387.  
  1388.         UNLESS (class_args = 
  1389.             PyObject_CallObject(getinitargs_func, empty_tuple))
  1390.             goto finally;
  1391.  
  1392.         if ((len = PyObject_Length(class_args)) < 0)  
  1393.             goto finally;
  1394.  
  1395.         for (i = 0; i < len; i++) {
  1396.             UNLESS (element = PySequence_GetItem(class_args, i)) 
  1397.                 goto finally;
  1398.  
  1399.             if (save(self, element, 0) < 0) {
  1400.                 Py_DECREF(element);
  1401.                 goto finally;
  1402.             }
  1403.  
  1404.             Py_DECREF(element);
  1405.         }
  1406.     }
  1407.     else {
  1408.         PyErr_Clear();
  1409.     }
  1410.  
  1411.     if (!self->bin) {
  1412.         UNLESS (name = ((PyClassObject *)class)->cl_name) {
  1413.             PyErr_SetString(PicklingError, "class has no name");
  1414.             goto finally;
  1415.         }
  1416.  
  1417.         UNLESS (module = whichmodule(class, name))
  1418.             goto finally;
  1419.  
  1420.         
  1421.         if ((module_size = PyString_Size(module)) < 0 ||
  1422.            (name_size = PyString_Size(name)) < 0)
  1423.           goto finally;
  1424.  
  1425.         module_str = PyString_AS_STRING((PyStringObject *)module);
  1426.         name_str   = PyString_AS_STRING((PyStringObject *)name);
  1427.  
  1428.         if ((*self->write_func)(self, &inst, 1) < 0)
  1429.             goto finally;
  1430.  
  1431.         if ((*self->write_func)(self, module_str, module_size) < 0)
  1432.             goto finally;
  1433.  
  1434.         if ((*self->write_func)(self, "\n", 1) < 0)
  1435.             goto finally;
  1436.  
  1437.         if ((*self->write_func)(self, name_str, name_size) < 0)
  1438.             goto finally;
  1439.  
  1440.         if ((*self->write_func)(self, "\n", 1) < 0)
  1441.             goto finally;
  1442.     }
  1443.     else if ((*self->write_func)(self, &obj, 1) < 0) {
  1444.         goto finally;
  1445.     }
  1446.  
  1447.     if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
  1448.         UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
  1449.             goto finally;
  1450.     }
  1451.     else {
  1452.         PyErr_Clear();
  1453.  
  1454.         UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
  1455.             PyErr_Clear();
  1456.             res = 0;
  1457.             goto finally;
  1458.         }
  1459.     }
  1460.  
  1461.     if (!PyDict_Check(state)) {
  1462.         if (put2(self, args) < 0)
  1463.             goto finally;
  1464.     }
  1465.     else {
  1466.         if (put(self, args) < 0)
  1467.             goto finally;
  1468.     }
  1469.   
  1470.     if (save(self, state, 0) < 0)
  1471.         goto finally;
  1472.  
  1473.     if ((*self->write_func)(self, &build, 1) < 0)
  1474.         goto finally;
  1475.  
  1476.     res = 0;
  1477.  
  1478. finally:
  1479.     Py_XDECREF(module);
  1480.     Py_XDECREF(class);
  1481.     Py_XDECREF(state);
  1482.     Py_XDECREF(getinitargs_func);
  1483.     Py_XDECREF(getstate_func);
  1484.     Py_XDECREF(class_args);
  1485.  
  1486.     return res;
  1487. }
  1488.  
  1489.  
  1490. static int
  1491. save_global(Picklerobject *self, PyObject *args, PyObject *name) {
  1492.     PyObject *global_name = 0, *module = 0;
  1493.     char *name_str, *module_str; 
  1494.     int module_size, name_size, res = -1;
  1495.  
  1496.     static char global = GLOBAL;
  1497.  
  1498.     if (name) {
  1499.         global_name = name;
  1500.         Py_INCREF(global_name);
  1501.     }
  1502.     else {
  1503.         UNLESS (global_name = PyObject_GetAttr(args, __name___str))
  1504.             goto finally;
  1505.     }
  1506.  
  1507.     UNLESS (module = whichmodule(args, global_name))
  1508.         goto finally;
  1509.  
  1510.     if ((module_size = PyString_Size(module)) < 0 ||
  1511.         (name_size = PyString_Size(global_name)) < 0)
  1512.       goto finally;
  1513.  
  1514.     module_str = PyString_AS_STRING((PyStringObject *)module);
  1515.     name_str   = PyString_AS_STRING((PyStringObject *)global_name);
  1516.  
  1517.     if ((*self->write_func)(self, &global, 1) < 0)
  1518.         goto finally;
  1519.  
  1520.     if ((*self->write_func)(self, module_str, module_size) < 0)
  1521.         goto finally;
  1522.  
  1523.     if ((*self->write_func)(self, "\n", 1) < 0)
  1524.         goto finally;
  1525.  
  1526.     if ((*self->write_func)(self, name_str, name_size) < 0)
  1527.         goto finally;
  1528.  
  1529.     if ((*self->write_func)(self, "\n", 1) < 0)
  1530.         goto finally;
  1531.  
  1532.     if (put(self, args) < 0)
  1533.         goto finally;
  1534.  
  1535.     res = 0;
  1536.  
  1537. finally:
  1538.     Py_XDECREF(module);
  1539.     Py_XDECREF(global_name);
  1540.  
  1541.     return res;
  1542. }
  1543.  
  1544. static int
  1545. save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
  1546.     PyObject *pid = 0;
  1547.     int size, res = -1;
  1548.  
  1549.     static char persid = PERSID, binpersid = BINPERSID;
  1550.  
  1551.     Py_INCREF(args);
  1552.     ARG_TUP(self, args);
  1553.     if (self->arg) {
  1554.         pid = PyObject_CallObject(f, self->arg);
  1555.         FREE_ARG_TUP(self);
  1556.     }
  1557.     if (! pid) return -1;
  1558.  
  1559.     if (pid != Py_None) {
  1560.         if (!self->bin) {
  1561.             if (!PyString_Check(pid)) {
  1562.                 PyErr_SetString(PicklingError, 
  1563.                     "persistent id must be string");
  1564.                 goto finally;
  1565.             }
  1566.  
  1567.             if ((*self->write_func)(self, &persid, 1) < 0)
  1568.                 goto finally;
  1569.  
  1570.             if ((size = PyString_Size(pid)) < 0)
  1571.                 goto finally;
  1572.  
  1573.             if ((*self->write_func)(self, 
  1574.                 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
  1575.                 goto finally;
  1576.  
  1577.             if ((*self->write_func)(self, "\n", 1) < 0)
  1578.                 goto finally;
  1579.      
  1580.             res = 1;
  1581.             goto finally;
  1582.         }
  1583.         else if (save(self, pid, 1) >= 0) {
  1584.             if ((*self->write_func)(self, &binpersid, 1) < 0)
  1585.                 res = -1;
  1586.             else
  1587.                 res = 1;
  1588.         }
  1589.  
  1590.         goto finally;              
  1591.     }
  1592.  
  1593.     res = 0;
  1594.  
  1595. finally:
  1596.     Py_XDECREF(pid);
  1597.  
  1598.     return res;
  1599. }
  1600.  
  1601.  
  1602. static int 
  1603. save_reduce(Picklerobject *self, PyObject *callable,
  1604.             PyObject *tup, PyObject *state, PyObject *ob) {
  1605.     static char reduce = REDUCE, build = BUILD;
  1606.  
  1607.     if (save(self, callable, 0) < 0)
  1608.         return -1;
  1609.  
  1610.     if (save(self, tup, 0) < 0)
  1611.         return -1;
  1612.  
  1613.     if ((*self->write_func)(self, &reduce, 1) < 0)
  1614.         return -1;
  1615.  
  1616.     if (ob != NULL) {
  1617.         if (state && !PyDict_Check(state)) {
  1618.             if (put2(self, ob) < 0)
  1619.                 return -1;
  1620.         }
  1621.         else {
  1622.             if (put(self, ob) < 0)
  1623.                 return -1;
  1624.         }
  1625.     }
  1626.     
  1627.     if (state) {
  1628.         if (save(self, state, 0) < 0)
  1629.             return -1;
  1630.  
  1631.         if ((*self->write_func)(self, &build, 1) < 0)
  1632.             return -1;
  1633.     }
  1634.  
  1635.     return 0;
  1636. }
  1637.  
  1638. static int
  1639. save(Picklerobject *self, PyObject *args, int  pers_save) {
  1640.     PyTypeObject *type;
  1641.     PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
  1642.              *callable = 0, *state = 0;
  1643.     int res = -1, tmp, size;
  1644.  
  1645.     if (!pers_save && self->pers_func) {
  1646.         if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
  1647.             res = tmp;
  1648.             goto finally;
  1649.         }
  1650.     }
  1651.  
  1652.     if (args == Py_None) {
  1653.         res = save_none(self, args);
  1654.         goto finally;
  1655.     }
  1656.  
  1657.     type = args->ob_type;
  1658.  
  1659.     switch (type->tp_name[0]) {
  1660.         case 'i':
  1661.             if (type == &PyInt_Type) {
  1662.                 res = save_int(self, args);
  1663.                 goto finally;
  1664.             }
  1665.             break;
  1666.  
  1667.         case 'l':
  1668.             if (type == &PyLong_Type) {
  1669.                 res = save_long(self, args);
  1670.                 goto finally;
  1671.             }
  1672.             break;
  1673.  
  1674.         case 'f':
  1675.             if (type == &PyFloat_Type) {
  1676.                 res = save_float(self, args);
  1677.                 goto finally;
  1678.             }
  1679.             break;
  1680.  
  1681.         case 't':
  1682.             if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
  1683.                 if (self->bin) res = save_empty_tuple(self, args);
  1684.                 else          res = save_tuple(self, args);
  1685.                 goto finally;
  1686.             }
  1687.             break;
  1688.  
  1689.         case 's':
  1690.             if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
  1691.                 res = save_string(self, args, 0);
  1692.                 goto finally;
  1693.             }
  1694.     }
  1695.  
  1696.     if (args->ob_refcnt > 1) {
  1697.         long ob_id;
  1698.         int  has_key;
  1699.  
  1700.         ob_id = (long)args;
  1701.  
  1702.         UNLESS (py_ob_id = PyInt_FromLong(ob_id))
  1703.             goto finally;
  1704.  
  1705.         if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
  1706.             goto finally;
  1707.  
  1708.         if (has_key) {
  1709.             if (get(self, py_ob_id) < 0)
  1710.                 goto finally;
  1711.  
  1712.             res = 0;
  1713.             goto finally;
  1714.         }
  1715.     }
  1716.  
  1717.     switch (type->tp_name[0]) {
  1718.         case 's':
  1719.             if (type == &PyString_Type) {
  1720.                 res = save_string(self, args, 1);
  1721.                 goto finally;
  1722.             }
  1723.             break;
  1724.  
  1725.         case 't':
  1726.             if (type == &PyTuple_Type) {
  1727.                 res = save_tuple(self, args);
  1728.                 goto finally;
  1729.             }
  1730.             break;
  1731.  
  1732.         case 'l':
  1733.             if (type == &PyList_Type) {
  1734.                 res = save_list(self, args);
  1735.                 goto finally;
  1736.             }
  1737.             break;
  1738.  
  1739.         case 'd':
  1740.             if (type == &PyDict_Type) {
  1741.                 res = save_dict(self, args);
  1742.                 goto finally; 
  1743.             }
  1744.             break;
  1745.  
  1746.         case 'i':
  1747.             if (type == &PyInstance_Type) {
  1748.                 res = save_inst(self, args);
  1749.                 goto finally;
  1750.             }
  1751.             break;
  1752.  
  1753.         case 'c':
  1754.             if (type == &PyClass_Type) {
  1755.                 res = save_global(self, args, NULL);
  1756.                 goto finally;
  1757.             }
  1758.             break;
  1759.  
  1760.         case 'f':
  1761.             if (type == &PyFunction_Type) {
  1762.                 res = save_global(self, args, NULL);
  1763.                 goto finally;
  1764.             }
  1765.             break;
  1766.  
  1767.         case 'b':
  1768.             if (type == &PyCFunction_Type) {
  1769.                 res = save_global(self, args, NULL);
  1770.                 goto finally;
  1771.             }
  1772.     }
  1773.  
  1774.     if (!pers_save && self->inst_pers_func) {
  1775.         if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
  1776.             res = tmp;
  1777.             goto finally;
  1778.         }
  1779.     }
  1780.  
  1781.     if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
  1782.         Py_INCREF(__reduce__);
  1783.  
  1784.         Py_INCREF(args);
  1785.         ARG_TUP(self, args);
  1786.         if (self->arg) {
  1787.             t = PyObject_CallObject(__reduce__, self->arg);
  1788.             FREE_ARG_TUP(self);
  1789.         }
  1790.         if (! t) goto finally;
  1791.     }        
  1792.     else {
  1793.         PyErr_Clear();
  1794.  
  1795.         if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
  1796.             UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
  1797.                 goto finally;
  1798.         }
  1799.         else {
  1800.             PyErr_Clear();
  1801.         }
  1802.     }
  1803.  
  1804.     if (t) {
  1805.         if (PyString_Check(t)) {
  1806.             res = save_global(self, args, t);
  1807.             goto finally;
  1808.         }
  1809.  
  1810.         if (!PyTuple_Check(t)) {
  1811.             cPickle_ErrFormat(PicklingError, "Value returned by %s must "
  1812.                 "be a tuple", "O", __reduce__);
  1813.             goto finally;
  1814.         }
  1815.  
  1816.         size = PyTuple_Size(t);
  1817.         
  1818.         if ((size != 3) && (size != 2)) {
  1819.             cPickle_ErrFormat(PicklingError, "tuple returned by %s must "     
  1820.                 "contain only two or three elements", "O", __reduce__);
  1821.                 goto finally;
  1822.         }
  1823.         
  1824.         callable = PyTuple_GET_ITEM(t, 0);
  1825.  
  1826.         arg_tup = PyTuple_GET_ITEM(t, 1);
  1827.  
  1828.         if (size > 2) {
  1829.             state = PyTuple_GET_ITEM(t, 2);
  1830.         }
  1831.  
  1832.         UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
  1833.             cPickle_ErrFormat(PicklingError, "Second element of tuple "
  1834.                 "returned by %s must be a tuple", "O", __reduce__);
  1835.             goto finally;
  1836.         }
  1837.  
  1838.         res = save_reduce(self, callable, arg_tup, state, args);
  1839.         goto finally;
  1840.     }
  1841.  
  1842.     cPickle_ErrFormat(PicklingError, "Cannot pickle %s objects.", 
  1843.         "O", (PyObject *)type);
  1844.  
  1845. finally:
  1846.     Py_XDECREF(py_ob_id);
  1847.     Py_XDECREF(__reduce__);
  1848.     Py_XDECREF(t);
  1849.    
  1850.     return res;
  1851. }
  1852.  
  1853.  
  1854. static int
  1855. dump(Picklerobject *self, PyObject *args) {
  1856.     static char stop = STOP;
  1857.  
  1858.     if (save(self, args, 0) < 0)
  1859.         return -1;
  1860.  
  1861.     if ((*self->write_func)(self, &stop, 1) < 0)
  1862.         return -1;
  1863.  
  1864.     if ((*self->write_func)(self, NULL, 0) < 0)
  1865.         return -1;
  1866.  
  1867.     return 0;
  1868. }
  1869.  
  1870. static PyObject *
  1871. Pickle_clear_memo(Picklerobject *self, PyObject *args) {
  1872.   if (args && ! PyArg_ParseTuple(args,"")) return NULL;
  1873.   if (self->memo) PyDict_Clear(self->memo);
  1874.   Py_INCREF(Py_None);
  1875.   return Py_None;
  1876. }
  1877.  
  1878. static PyObject *
  1879. Pickle_getvalue(Picklerobject *self, PyObject *args) {
  1880.   int l, i, rsize, ssize, clear=1, lm;
  1881.   long ik;
  1882.   PyObject *k, *r;
  1883.   char *s, *p, *have_get;
  1884.   Pdata *data;
  1885.  
  1886.   if (args && ! PyArg_ParseTuple(args,"|i",&clear)) return NULL;
  1887.  
  1888.   /* Check to make sure we are based on a list */
  1889.   if (! Pdata_Check(self->file)) {
  1890.       PyErr_SetString(PicklingError,
  1891.                       "Attempt to getvalue a non-list-based pickler");
  1892.       return NULL;
  1893.     }
  1894.  
  1895.   /* flush write buffer */
  1896.   if (write_other(self, NULL, 0) < 0) return NULL;
  1897.  
  1898.   data=(Pdata*)self->file;
  1899.   l=data->length;
  1900.  
  1901.   /* set up an array to hold get/put status */
  1902.   if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
  1903.   lm++;
  1904.   if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();  
  1905.   memset(have_get,0,lm);
  1906.  
  1907.   /* Scan for gets. */
  1908.   for (rsize=0, i=l; --i >= 0; ) {
  1909.       k=data->data[i];
  1910.       
  1911.       if (PyString_Check(k)) {
  1912.         rsize += PyString_GET_SIZE(k);
  1913.       }
  1914.  
  1915.       else if (PyInt_Check(k)) { /* put */
  1916.         ik=PyInt_AS_LONG((PyIntObject*)k);
  1917.         if (ik >= lm || ik==0) {
  1918.           PyErr_SetString(PicklingError,
  1919.                           "Invalid get data");
  1920.           return NULL;
  1921.         }      
  1922.         if (have_get[ik]) { /* with matching get */
  1923.           if (ik < 256) rsize += 2;
  1924.           else rsize+=5;
  1925.         }
  1926.       }
  1927.  
  1928.       else if (! (PyTuple_Check(k) &&
  1929.                   PyTuple_GET_SIZE(k) == 2 &&
  1930.                   PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
  1931.                ) {
  1932.         PyErr_SetString(PicklingError,
  1933.                         "Unexpected data in internal list");
  1934.         return NULL;
  1935.       }
  1936.  
  1937.       else { /* put */
  1938.         ik=PyInt_AS_LONG((PyIntObject*)k);
  1939.         if (ik >= lm || ik==0) {
  1940.           PyErr_SetString(PicklingError,
  1941.                           "Invalid get data");
  1942.           return NULL;
  1943.         }      
  1944.         have_get[ik]=1;
  1945.         if (ik < 256) rsize += 2;
  1946.         else rsize+=5;
  1947.       }
  1948.  
  1949.     }
  1950.  
  1951.   /* Now generate the result */
  1952.   UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
  1953.   s=PyString_AS_STRING((PyStringObject*)r);
  1954.  
  1955.   for (i=0; i<l; i++) {
  1956.       k=data->data[i];
  1957.  
  1958.       if (PyString_Check(k)) {
  1959.         ssize=PyString_GET_SIZE(k);
  1960.         if (ssize) {
  1961.             p=PyString_AS_STRING((PyStringObject*)k);
  1962.             while (--ssize >= 0) *s++=*p++;
  1963.           }
  1964.       }
  1965.  
  1966.       else if (PyTuple_Check(k)) { /* get */
  1967.         ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
  1968.         if (ik < 256) {
  1969.           *s++ = BINGET;
  1970.           *s++ = (int)(ik & 0xff);
  1971.         }
  1972.         else {
  1973.           *s++ = LONG_BINGET;
  1974.           *s++ = (int)(ik & 0xff);
  1975.           *s++ = (int)((ik >> 8)  & 0xff);
  1976.           *s++ = (int)((ik >> 16) & 0xff);
  1977.           *s++ = (int)((ik >> 24) & 0xff);
  1978.         }
  1979.       }
  1980.  
  1981.       else { /* put */
  1982.         ik=PyInt_AS_LONG((PyIntObject*)k);
  1983.  
  1984.         if (have_get[ik]) { /* with matching get */
  1985.           if (ik < 256) {
  1986.             *s++ = BINPUT;
  1987.             *s++ = (int)(ik & 0xff);
  1988.           }
  1989.           else {
  1990.             *s++ = LONG_BINPUT;
  1991.             *s++ = (int)(ik & 0xff);
  1992.             *s++ = (int)((ik >> 8)  & 0xff);
  1993.             *s++ = (int)((ik >> 16) & 0xff);
  1994.             *s++ = (int)((ik >> 24) & 0xff);
  1995.           }
  1996.         }
  1997.       }
  1998.  
  1999.     }
  2000.  
  2001.   if (clear) {
  2002.     PyDict_Clear(self->memo);
  2003.     Pdata_clear(data,0);
  2004.   }
  2005.     
  2006.   free(have_get);
  2007.   return r;
  2008. err:
  2009.   free(have_get);
  2010.   return NULL;
  2011. }
  2012.  
  2013. static PyObject *
  2014. Pickler_dump(Picklerobject *self, PyObject *args) {
  2015.     PyObject *ob;
  2016.     int get=0;
  2017.  
  2018.     UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &get))
  2019.         return NULL;
  2020.  
  2021.     if (dump(self, ob) < 0)
  2022.         return NULL;
  2023.  
  2024.     if (get) return Pickle_getvalue(self, NULL);
  2025.  
  2026.     Py_INCREF(self);
  2027.     return (PyObject*)self;
  2028. }
  2029.  
  2030.  
  2031. static struct PyMethodDef Pickler_methods[] = {
  2032.   {"dump",          (PyCFunction)Pickler_dump,  1,
  2033.    "dump(object) --"
  2034.    "Write an object in pickle format to the object's pickle stream\n"
  2035.   },
  2036.   {"clear_memo",  (PyCFunction)Pickle_clear_memo,  1,
  2037.    "clear_memo() -- Clear the picklers memo"},
  2038.   {"getvalue",  (PyCFunction)Pickle_getvalue,  1,
  2039.    "getvalue() -- Finish picking a list-based pickle"},
  2040.   {NULL,                NULL}           /* sentinel */
  2041. };
  2042.  
  2043.  
  2044. static Picklerobject *
  2045. newPicklerobject(PyObject *file, int bin) {
  2046.     Picklerobject *self;
  2047.  
  2048.     UNLESS (self = PyObject_NEW(Picklerobject, &Picklertype))
  2049.         return NULL;
  2050.  
  2051.     self->fp = NULL;
  2052.     self->write = NULL;
  2053.     self->memo = NULL;
  2054.     self->arg = NULL;
  2055.     self->pers_func = NULL;
  2056.     self->inst_pers_func = NULL;
  2057.     self->write_buf = NULL;
  2058.     self->bin = bin;
  2059.     self->fast = 0;
  2060.     self->buf_size = 0;
  2061.     self->dispatch_table = NULL;
  2062.  
  2063.     if (file)
  2064.       Py_INCREF(file);
  2065.     else
  2066.       file=Pdata_New();
  2067.  
  2068.     self->file = file;
  2069.  
  2070.     UNLESS (self->memo = PyDict_New()) {
  2071.        Py_XDECREF((PyObject *)self);
  2072.        return NULL;
  2073.     }
  2074.  
  2075.     if (PyFile_Check(file)) {
  2076.         self->fp = PyFile_AsFile(file);
  2077.     if (self->fp == NULL) {
  2078.         PyErr_SetString(PyExc_IOError, "output file closed");
  2079.         return NULL;
  2080.     }
  2081.         self->write_func = write_file;
  2082.     }
  2083.     else if (PycStringIO_OutputCheck(file)) {
  2084.         self->write_func = write_cStringIO;
  2085.     }
  2086.     else if (file == Py_None) {
  2087.         self->write_func = write_none;
  2088.     }
  2089.     else {
  2090.         self->write_func = write_other;
  2091.  
  2092.         if (! Pdata_Check(file)) {
  2093.           UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
  2094.             PyErr_Clear();
  2095.             PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
  2096.                             "attribute");
  2097.             goto err;
  2098.           }
  2099.         }
  2100.  
  2101.         UNLESS (self->write_buf = 
  2102.             (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) { 
  2103.             PyErr_NoMemory();
  2104.             goto err;
  2105.         }
  2106.     }
  2107.  
  2108.     if (PyEval_GetRestricted()) {
  2109.         /* Restricted execution, get private tables */
  2110.         PyObject *m;
  2111.  
  2112.         UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
  2113.         self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
  2114.         Py_DECREF(m);
  2115.         UNLESS (self->dispatch_table) goto err;
  2116.     }
  2117.     else {
  2118.         self->dispatch_table=dispatch_table;
  2119.         Py_INCREF(dispatch_table);
  2120.     }
  2121.  
  2122.     return self;
  2123.  
  2124. err:
  2125.     Py_DECREF((PyObject *)self);
  2126.     return NULL;
  2127. }
  2128.  
  2129.  
  2130. static PyObject *
  2131. get_Pickler(PyObject *self, PyObject *args) {
  2132.     PyObject *file=NULL;
  2133.     int bin;
  2134.  
  2135.     bin=1;
  2136.     if (! PyArg_ParseTuple(args, "|i", &bin)) {
  2137.         PyErr_Clear();
  2138.         bin=0;
  2139.         if (! PyArg_ParseTuple(args, "O|i", &file, &bin))
  2140.           return NULL;
  2141.       }
  2142.     return (PyObject *)newPicklerobject(file, bin);
  2143. }
  2144.  
  2145.  
  2146. static void
  2147. Pickler_dealloc(Picklerobject *self) {
  2148.     Py_XDECREF(self->write);
  2149.     Py_XDECREF(self->memo);
  2150.     Py_XDECREF(self->arg);
  2151.     Py_XDECREF(self->file);
  2152.     Py_XDECREF(self->pers_func);
  2153.     Py_XDECREF(self->inst_pers_func);
  2154.     Py_XDECREF(self->dispatch_table);
  2155.  
  2156.     if (self->write_buf) {    
  2157.         free(self->write_buf);
  2158.     }
  2159.  
  2160.     PyMem_DEL(self);
  2161. }
  2162.  
  2163.  
  2164. static PyObject *
  2165. Pickler_getattr(Picklerobject *self, char *name) {
  2166.  
  2167.   switch (*name) {
  2168.   case 'p':
  2169.     if (strcmp(name, "persistent_id") == 0) {
  2170.         if (!self->pers_func) {
  2171.             PyErr_SetString(PyExc_AttributeError, name);
  2172.             return NULL;
  2173.         }
  2174.  
  2175.         Py_INCREF(self->pers_func);
  2176.         return self->pers_func;
  2177.     }
  2178.     break;
  2179.   case 'm':
  2180.     if (strcmp(name, "memo") == 0) {
  2181.         if (!self->memo) {
  2182.             PyErr_SetString(PyExc_AttributeError, name);
  2183.             return NULL;
  2184.         }
  2185.  
  2186.         Py_INCREF(self->memo);
  2187.         return self->memo;
  2188.     }
  2189.     break;
  2190.   case 'P':
  2191.     if (strcmp(name, "PicklingError") == 0) {
  2192.         Py_INCREF(PicklingError);
  2193.         return PicklingError;
  2194.     }
  2195.     break;
  2196.   case 'b':
  2197.     if (strcmp(name, "binary")==0)
  2198.       return PyInt_FromLong(self->bin);
  2199.     break;
  2200.   case 'f':
  2201.     if (strcmp(name, "fast")==0)
  2202.       return PyInt_FromLong(self->fast);
  2203.     break;
  2204.   case 'g':
  2205.     if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
  2206.       PyErr_SetString(PyExc_AttributeError, name);
  2207.       return NULL;
  2208.     }
  2209.     break;
  2210.   }
  2211.   return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
  2212. }
  2213.  
  2214.  
  2215. int 
  2216. Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
  2217.  
  2218.     if (! value) {
  2219.         PyErr_SetString(PyExc_TypeError,
  2220.                         "attribute deletion is not supported");
  2221.         return -1;
  2222.     }
  2223.   
  2224.     if (strcmp(name, "persistent_id") == 0) {
  2225.         Py_XDECREF(self->pers_func);
  2226.         self->pers_func = value;
  2227.         Py_INCREF(value);
  2228.         return 0;
  2229.     }
  2230.  
  2231.     if (strcmp(name, "inst_persistent_id") == 0) {
  2232.         Py_XDECREF(self->inst_pers_func);
  2233.         self->inst_pers_func = value;
  2234.         Py_INCREF(value);
  2235.         return 0;
  2236.     }
  2237.  
  2238.     if (strcmp(name, "memo") == 0) {
  2239.         if (! PyDict_Check(value)) {
  2240.           PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
  2241.           return -1;
  2242.         }
  2243.         Py_XDECREF(self->memo);
  2244.         self->memo = value;
  2245.         Py_INCREF(value);
  2246.         return 0;
  2247.     }
  2248.  
  2249.     if (strcmp(name, "binary")==0) {
  2250.         self->bin=PyObject_IsTrue(value);
  2251.         return 0;
  2252.     }
  2253.  
  2254.     if (strcmp(name, "fast")==0) {
  2255.         self->fast=PyObject_IsTrue(value);
  2256.         return 0;
  2257.     }
  2258.  
  2259.     PyErr_SetString(PyExc_AttributeError, name);
  2260.     return -1;
  2261. }
  2262.  
  2263.  
  2264. static char Picklertype__doc__[] =
  2265. "Objects that know how to pickle objects\n"
  2266. ;
  2267.  
  2268. static PyTypeObject Picklertype = {
  2269.     PyObject_HEAD_INIT(NULL)
  2270.     0,                            /*ob_size*/
  2271.     "Pickler",                    /*tp_name*/
  2272.     sizeof(Picklerobject),                /*tp_basicsize*/
  2273.     0,                            /*tp_itemsize*/
  2274.     /* methods */
  2275.     (destructor)Pickler_dealloc,  /*tp_dealloc*/
  2276.     (printfunc)0,         /*tp_print*/
  2277.     (getattrfunc)Pickler_getattr, /*tp_getattr*/
  2278.     (setattrfunc)Pickler_setattr, /*tp_setattr*/
  2279.     (cmpfunc)0,           /*tp_compare*/
  2280.     (reprfunc)0,          /*tp_repr*/
  2281.     0,                    /*tp_as_number*/
  2282.     0,            /*tp_as_sequence*/
  2283.     0,            /*tp_as_mapping*/
  2284.     (hashfunc)0,          /*tp_hash*/
  2285.     (ternaryfunc)0,               /*tp_call*/
  2286.     (reprfunc)0,          /*tp_str*/
  2287.  
  2288.     /* Space for future expansion */
  2289.     0L,0L,0L,0L,
  2290.     Picklertype__doc__ /* Documentation string */
  2291. };
  2292.  
  2293. static PyObject *
  2294. find_class(PyObject *py_module_name, PyObject *py_global_name) {
  2295.     PyObject *global = 0, *module;
  2296.  
  2297.     module = PySys_GetObject("modules");
  2298.     if (module == NULL)
  2299.       return NULL;
  2300.  
  2301.     module = PyDict_GetItem(module, py_module_name);
  2302.     if (module == NULL) {
  2303.       module = PyImport_Import(py_module_name);
  2304.       if (!module)
  2305.           return NULL;
  2306.       global = PyObject_GetAttr(module, py_global_name);
  2307.       Py_DECREF(module);
  2308.     }
  2309.     else
  2310.       global = PyObject_GetAttr(module, py_global_name);
  2311.     if (global == NULL) {
  2312.       char buf[256 + 37];
  2313.       sprintf(buf, "Failed to import class %.128s from module %.128s",
  2314.               PyString_AS_STRING((PyStringObject*)py_global_name),
  2315.               PyString_AS_STRING((PyStringObject*)py_module_name));  
  2316.       PyErr_SetString(PyExc_SystemError, buf);
  2317.       return NULL;
  2318.     }
  2319.     return global;
  2320. }
  2321.  
  2322. static int
  2323. marker(Unpicklerobject *self) {
  2324.     if (self->num_marks < 1) {
  2325.         PyErr_SetString(UnpicklingError, "could not find MARK");
  2326.         return -1;
  2327.     }
  2328.  
  2329.     return self->marks[--self->num_marks];
  2330. }
  2331.  
  2332.     
  2333. static int
  2334. load_none(Unpicklerobject *self) {
  2335.     PDATA_APPEND(self->stack, Py_None, -1);
  2336.     return 0;
  2337. }
  2338.  
  2339. static int
  2340. bad_readline() {
  2341.     PyErr_SetString(UnpicklingError, "pickle data was truncated");
  2342.     return -1;
  2343. }
  2344.  
  2345. static int
  2346. load_int(Unpicklerobject *self) {
  2347.     PyObject *py_int = 0;
  2348.     char *endptr, *s;
  2349.     int len, res = -1;
  2350.     long l;
  2351.  
  2352.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2353.     if (len < 2) return bad_readline();
  2354.     UNLESS (s=pystrndup(s,len)) return -1;
  2355.  
  2356.     errno = 0;
  2357.     l = strtol(s, &endptr, 0);
  2358.  
  2359.     if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
  2360.         /* Hm, maybe we've got something long.  Let's try reading
  2361.            it as a Python long object. */
  2362.         errno=0;
  2363.         UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
  2364.  
  2365.         if ((*endptr != '\n') || (endptr[1] != '\0')) {
  2366.             PyErr_SetString(PyExc_ValueError,
  2367.                             "could not convert string to int");
  2368.             goto finally;
  2369.         }
  2370.     }
  2371.     else {
  2372.         UNLESS (py_int = PyInt_FromLong(l)) goto finally;
  2373.     }
  2374.  
  2375.     free(s);
  2376.     PDATA_PUSH(self->stack, py_int, -1);
  2377.     return 0;
  2378.  
  2379. finally:
  2380.     free(s);
  2381.  
  2382.     return res;
  2383. }
  2384.  
  2385.  
  2386. static long 
  2387. calc_binint(char *s, int  x) {
  2388.     unsigned char c;
  2389.     int i;
  2390.     long l;
  2391.  
  2392.     for (i = 0, l = 0L; i < x; i++) {
  2393.         c = (unsigned char)s[i];
  2394.         l |= (long)c << (i * 8);
  2395.     }
  2396.  
  2397.     return l;
  2398. }
  2399.  
  2400.  
  2401. static int
  2402. load_binintx(Unpicklerobject *self, char *s, int  x) {
  2403.     PyObject *py_int = 0;
  2404.     long l;
  2405.  
  2406.     l = calc_binint(s, x);
  2407.  
  2408.     UNLESS (py_int = PyInt_FromLong(l))
  2409.         return -1;
  2410.  
  2411.     PDATA_PUSH(self->stack, py_int, -1);
  2412.     return 0;
  2413. }
  2414.  
  2415.  
  2416. static int
  2417. load_binint(Unpicklerobject *self) {
  2418.     char *s;
  2419.  
  2420.     if ((*self->read_func)(self, &s, 4) < 0)
  2421.         return -1;
  2422.  
  2423.     return load_binintx(self, s, 4);
  2424. }
  2425.  
  2426.  
  2427. static int
  2428. load_binint1(Unpicklerobject *self) {
  2429.     char *s;
  2430.  
  2431.     if ((*self->read_func)(self, &s, 1) < 0)
  2432.         return -1;
  2433.  
  2434.     return load_binintx(self, s, 1);
  2435. }
  2436.  
  2437.  
  2438. static int
  2439. load_binint2(Unpicklerobject *self) {
  2440.     char *s;
  2441.  
  2442.     if ((*self->read_func)(self, &s, 2) < 0)
  2443.         return -1;
  2444.  
  2445.     return load_binintx(self, s, 2);
  2446. }
  2447.     
  2448. static int
  2449. load_long(Unpicklerobject *self) {
  2450.     PyObject *l = 0;
  2451.     char *end, *s;
  2452.     int len, res = -1;
  2453.  
  2454.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2455.     if (len < 2) return bad_readline();
  2456.     UNLESS (s=pystrndup(s,len)) return -1;
  2457.  
  2458.     UNLESS (l = PyLong_FromString(s, &end, 0))
  2459.         goto finally;
  2460.  
  2461.     free(s);
  2462.     PDATA_PUSH(self->stack, l, -1);
  2463.     return 0;
  2464.  
  2465. finally:
  2466.     free(s);
  2467.  
  2468.     return res;
  2469. }
  2470.  
  2471.  
  2472. static int
  2473. load_float(Unpicklerobject *self) {
  2474.     PyObject *py_float = 0;
  2475.     char *endptr, *s;
  2476.     int len, res = -1;
  2477.     double d;
  2478.  
  2479.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2480.     if (len < 2) return bad_readline();
  2481.     UNLESS (s=pystrndup(s,len)) return -1;
  2482.  
  2483.     errno = 0;
  2484.     d = strtod(s, &endptr);
  2485.  
  2486.     if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
  2487.         PyErr_SetString(PyExc_ValueError, 
  2488.         "could not convert string to float");
  2489.         goto finally;
  2490.     }
  2491.  
  2492.     UNLESS (py_float = PyFloat_FromDouble(d))
  2493.         goto finally;
  2494.  
  2495.     free(s);
  2496.     PDATA_PUSH(self->stack, py_float, -1);
  2497.     return 0;
  2498.  
  2499. finally:
  2500.     free(s);
  2501.  
  2502.     return res;
  2503. }
  2504.  
  2505. static int
  2506. load_binfloat(Unpicklerobject *self) {
  2507.     PyObject *py_float = 0;
  2508.     int s, e, res = -1;
  2509.     long fhi, flo;
  2510.     double x;
  2511.     char *p;
  2512.  
  2513.     if ((*self->read_func)(self, &p, 8) < 0)
  2514.         return -1;
  2515.  
  2516.     /* First byte */
  2517.     s = (*p>>7) & 1;
  2518.     e = (*p & 0x7F) << 4;
  2519.     p++;
  2520.  
  2521.     /* Second byte */
  2522.     e |= (*p>>4) & 0xF;
  2523.     fhi = (*p & 0xF) << 24;
  2524.     p++;
  2525.  
  2526.     /* Third byte */
  2527.     fhi |= (*p & 0xFF) << 16;
  2528.     p++;
  2529.  
  2530.     /* Fourth byte */
  2531.     fhi |= (*p & 0xFF) << 8;
  2532.     p++;
  2533.  
  2534.     /* Fifth byte */
  2535.     fhi |= *p & 0xFF;
  2536.     p++;
  2537.  
  2538.     /* Sixth byte */
  2539.     flo = (*p & 0xFF) << 16;
  2540.     p++;
  2541.  
  2542.     /* Seventh byte */
  2543.     flo |= (*p & 0xFF) << 8;
  2544.     p++;
  2545.  
  2546.     /* Eighth byte */
  2547.     flo |= *p & 0xFF;
  2548.  
  2549.     x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
  2550.     x /= 268435456.0; /* 2**28 */
  2551.  
  2552.     /* XXX This sadly ignores Inf/NaN */
  2553.     if (e == 0)
  2554.         e = -1022;
  2555.     else {
  2556.         x += 1.0;
  2557.         e -= 1023;
  2558.     }
  2559.     x = ldexp(x, e);
  2560.  
  2561.     if (s)
  2562.         x = -x;
  2563.  
  2564.     UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
  2565.  
  2566.     PDATA_PUSH(self->stack, py_float, -1);
  2567.     return 0;
  2568. }
  2569.  
  2570. static int
  2571. load_string(Unpicklerobject *self) {
  2572.     PyObject *str = 0;
  2573.     int len, res = -1, nslash;
  2574.     char *s, q, *p;
  2575.  
  2576.     static PyObject *eval_dict = 0;
  2577.  
  2578.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2579.     if (len < 2) return bad_readline();
  2580.     UNLESS (s=pystrndup(s,len)) return -1;
  2581.  
  2582.     /* Check for unquoted quotes (evil strings) */
  2583.     q=*s;
  2584.     if (q != '"' && q != '\'') goto insecure;
  2585.     for (p=s+1, nslash=0; *p; p++) {
  2586.         if (*p==q && nslash%2==0) break;
  2587.         if (*p=='\\') nslash++;
  2588.         else nslash=0;
  2589.       }
  2590.     if (*p==q)
  2591.       {
  2592.         for (p++; *p; p++) if (*p > ' ') goto insecure;
  2593.       }
  2594.     else goto insecure;
  2595.     /********************************************/
  2596.  
  2597.     UNLESS (eval_dict)
  2598.         UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
  2599.             goto finally;
  2600.  
  2601.     UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
  2602.         goto finally;
  2603.  
  2604.     free(s);
  2605.     PDATA_PUSH(self->stack, str, -1);
  2606.     return 0;
  2607.  
  2608. finally:
  2609.     free(s);
  2610.  
  2611.     return res;
  2612.     
  2613. insecure:
  2614.     free(s);
  2615.     PyErr_SetString(PyExc_ValueError,"insecure string pickle");
  2616.     return -1;
  2617.  
  2618.  
  2619. static int
  2620. load_binstring(Unpicklerobject *self) {
  2621.     PyObject *py_string = 0;
  2622.     long l;
  2623.     int res = -1;
  2624.     char *s;
  2625.  
  2626.     if ((*self->read_func)(self, &s, 4) < 0) return -1;
  2627.  
  2628.     l = calc_binint(s, 4);
  2629.  
  2630.     if ((*self->read_func)(self, &s, l) < 0)
  2631.         return -1;
  2632.  
  2633.     UNLESS (py_string = PyString_FromStringAndSize(s, l))
  2634.         return -1;
  2635.  
  2636.     PDATA_PUSH(self->stack, py_string, -1);
  2637.     return 0;
  2638. }
  2639.  
  2640.  
  2641. static int
  2642. load_short_binstring(Unpicklerobject *self) {
  2643.     PyObject *py_string = 0;
  2644.     unsigned char l;  
  2645.     int res = -1;
  2646.     char *s;
  2647.  
  2648.     if ((*self->read_func)(self, &s, 1) < 0)
  2649.         return -1;
  2650.  
  2651.     l = (unsigned char)s[0];
  2652.  
  2653.     if ((*self->read_func)(self, &s, l) < 0) return -1;
  2654.  
  2655.     UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
  2656.  
  2657.     PDATA_PUSH(self->stack, py_string, -1);
  2658.     return 0;
  2659.  
  2660.  
  2661. static int
  2662. load_tuple(Unpicklerobject *self) {
  2663.     PyObject *tup;
  2664.     int i;
  2665.  
  2666.     if ((i = marker(self)) < 0) return -1;
  2667.     UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
  2668.     PDATA_PUSH(self->stack, tup, -1);
  2669.     return 0;
  2670. }
  2671.  
  2672. static int
  2673. load_empty_tuple(Unpicklerobject *self) {
  2674.     PyObject *tup;
  2675.  
  2676.     UNLESS (tup=PyTuple_New(0)) return -1;
  2677.     PDATA_PUSH(self->stack, tup, -1);
  2678.     return 0;
  2679. }
  2680.  
  2681. static int
  2682. load_empty_list(Unpicklerobject *self) {
  2683.     PyObject *list;
  2684.  
  2685.     UNLESS (list=PyList_New(0)) return -1;
  2686.     PDATA_PUSH(self->stack, list, -1);
  2687.     return 0;
  2688. }
  2689.  
  2690. static int
  2691. load_empty_dict(Unpicklerobject *self) {
  2692.     PyObject *dict;
  2693.  
  2694.     UNLESS (dict=PyDict_New()) return -1;
  2695.     PDATA_PUSH(self->stack, dict, -1);
  2696.     return 0;
  2697. }
  2698.  
  2699.  
  2700. static int
  2701. load_list(Unpicklerobject *self) {
  2702.     PyObject *list = 0;
  2703.     int i;
  2704.  
  2705.     if ((i = marker(self)) < 0) return -1;
  2706.     UNLESS (list=Pdata_popList(self->stack, i)) return -1;
  2707.     PDATA_PUSH(self->stack, list, -1);
  2708.     return 0;
  2709. }
  2710.  
  2711. static int
  2712. load_dict(Unpicklerobject *self) {
  2713.     PyObject *dict, *key, *value;
  2714.     int i, j, k;
  2715.  
  2716.     if ((i = marker(self)) < 0) return -1;
  2717.     j=self->stack->length;
  2718.  
  2719.     UNLESS (dict = PyDict_New()) return -1;
  2720.  
  2721.     for (k = i+1; k < j; k += 2) {
  2722.         key  =self->stack->data[k-1];
  2723.         value=self->stack->data[k  ];
  2724.         if (PyDict_SetItem(dict, key, value) < 0) {
  2725.             Py_DECREF(dict);
  2726.             return -1;
  2727.         }
  2728.     }
  2729.     Pdata_clear(self->stack, i);
  2730.     PDATA_PUSH(self->stack, dict, -1);
  2731.     return 0;
  2732. }
  2733.  
  2734. static PyObject *
  2735. Instance_New(PyObject *cls, PyObject *args) {
  2736.   int has_key;
  2737.   PyObject *safe=0, *r=0;
  2738.  
  2739.   if (PyClass_Check(cls)) {
  2740.       int l;
  2741.       
  2742.       if ((l=PyObject_Length(args)) < 0) goto err;
  2743.       UNLESS (l) {
  2744.           PyObject *__getinitargs__;
  2745.  
  2746.           UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
  2747.               /* We have a class with no __getinitargs__, so bypass usual
  2748.                  construction  */
  2749.               PyInstanceObject *inst;
  2750.  
  2751.               PyErr_Clear();
  2752.               UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
  2753.                 goto err;
  2754.               inst->in_class=(PyClassObject*)cls;
  2755.               Py_INCREF(cls);
  2756.               UNLESS (inst->in_dict=PyDict_New()) {
  2757.                 Py_DECREF(inst);
  2758.                 goto err;
  2759.               }
  2760.  
  2761.               return (PyObject *)inst;
  2762.             }
  2763.           Py_DECREF(__getinitargs__);
  2764.         }
  2765.       
  2766.       if ((r=PyInstance_New(cls, args, NULL))) return r;
  2767.       else goto err;
  2768.     }
  2769.        
  2770.   
  2771.   if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
  2772.     goto err;
  2773.     
  2774.   if (!has_key)
  2775.     if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
  2776.        !PyObject_IsTrue(safe)) {
  2777.       cPickle_ErrFormat(UnpicklingError,
  2778.                         "%s is not safe for unpickling", "O", cls);
  2779.       Py_XDECREF(safe);
  2780.       return NULL;
  2781.   }
  2782.  
  2783.   if (args==Py_None) {
  2784.       /* Special case, call cls.__basicnew__() */
  2785.       PyObject *basicnew;
  2786.       
  2787.       UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
  2788.       r=PyObject_CallObject(basicnew, NULL);
  2789.       Py_DECREF(basicnew);
  2790.       if (r) return r;
  2791.     }
  2792.  
  2793.   if ((r=PyObject_CallObject(cls, args))) return r;
  2794.  
  2795. err:
  2796.   {
  2797.     PyObject *tp, *v, *tb;
  2798.  
  2799.     PyErr_Fetch(&tp, &v, &tb);
  2800.     if ((r=Py_BuildValue("OOO",v,cls,args))) {
  2801.         Py_XDECREF(v);
  2802.         v=r;
  2803.       }
  2804.     PyErr_Restore(tp,v,tb);
  2805.   }
  2806.   return NULL;
  2807. }
  2808.   
  2809.  
  2810. static int
  2811. load_obj(Unpicklerobject *self) {
  2812.     PyObject *class, *tup, *obj=0;
  2813.     int i;
  2814.  
  2815.     if ((i = marker(self)) < 0) return -1;
  2816.     UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
  2817.     PDATA_POP(self->stack, class);
  2818.     if (class) {
  2819.         obj = Instance_New(class, tup);
  2820.         Py_DECREF(class);
  2821.     }
  2822.     Py_DECREF(tup);
  2823.  
  2824.     if (! obj) return -1;
  2825.     PDATA_PUSH(self->stack, obj, -1);
  2826.     return 0;
  2827. }
  2828.  
  2829.  
  2830. static int
  2831. load_inst(Unpicklerobject *self) {
  2832.     PyObject *tup, *class, *obj, *module_name, *class_name;
  2833.     int i, len;
  2834.     char *s;
  2835.  
  2836.     if ((i = marker(self)) < 0) return -1;
  2837.  
  2838.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2839.     if (len < 2) return bad_readline();
  2840.     UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
  2841.  
  2842.     if ((len = (*self->readline_func)(self, &s)) >= 0) {
  2843.         if (len < 2) return bad_readline();
  2844.         if (class_name = PyString_FromStringAndSize(s, len - 1)) {
  2845.             class = find_class(module_name, class_name);
  2846.             Py_DECREF(class_name);
  2847.         }
  2848.     }
  2849.     Py_DECREF(module_name);
  2850.  
  2851.     if (! class) return -1;
  2852.       
  2853.     if (tup=Pdata_popTuple(self->stack, i)) {
  2854.         obj = Instance_New(class, tup);
  2855.         Py_DECREF(tup);
  2856.     }
  2857.     Py_DECREF(class);
  2858.  
  2859.     if (! obj) return -1;
  2860.  
  2861.     PDATA_PUSH(self->stack, obj, -1);
  2862.     return 0;
  2863. }
  2864.  
  2865.  
  2866. static int
  2867. load_global(Unpicklerobject *self) {
  2868.     PyObject *class = 0, *module_name = 0, *class_name = 0;
  2869.     int len;
  2870.     char *s;
  2871.  
  2872.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2873.     if (len < 2) return bad_readline();
  2874.     UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
  2875.  
  2876.     if ((len = (*self->readline_func)(self, &s)) >= 0) {
  2877.         if (len < 2) return bad_readline();
  2878.         if (class_name = PyString_FromStringAndSize(s, len - 1)) {
  2879.             class = find_class(module_name, class_name);
  2880.             Py_DECREF(class_name);
  2881.         }
  2882.     }
  2883.     Py_DECREF(module_name);
  2884.  
  2885.     if (! class) return -1;
  2886.     PDATA_PUSH(self->stack, class, -1);
  2887.     return 0;
  2888. }
  2889.  
  2890.  
  2891. static int
  2892. load_persid(Unpicklerobject *self) {
  2893.     PyObject *pid = 0;
  2894.     int len, res = -1;
  2895.     char *s;
  2896.  
  2897.     if (self->pers_func) {
  2898.         if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2899.     if (len < 2) return bad_readline();
  2900.   
  2901.         UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
  2902.  
  2903.         if (PyList_Check(self->pers_func)) {
  2904.             if (PyList_Append(self->pers_func, pid) < 0) {
  2905.                 Py_DECREF(pid);
  2906.                 return -1;
  2907.             }
  2908.         }
  2909.         else {
  2910.             ARG_TUP(self, pid);
  2911.             if (self->arg) {
  2912.                 pid = PyObject_CallObject(self->pers_func, self->arg);
  2913.                 FREE_ARG_TUP(self);
  2914.             }
  2915.         }
  2916.  
  2917.         if (! pid) return -1;
  2918.  
  2919.         PDATA_PUSH(self->stack, pid, -1);
  2920.         return 0;
  2921.     }
  2922.     else {
  2923.       PyErr_SetString(UnpicklingError,
  2924.                       "A load persistent id instruction was encountered,\n"
  2925.                       "but no persistent_load function was specified.");
  2926.       return -1;
  2927.     }
  2928. }
  2929.  
  2930. static int
  2931. load_binpersid(Unpicklerobject *self) {
  2932.     PyObject *pid = 0;
  2933.     int res = -1;
  2934.  
  2935.     if (self->pers_func) {
  2936.         PDATA_POP(self->stack, pid);
  2937.         if (! pid) return -1;
  2938.  
  2939.         if (PyList_Check(self->pers_func)) {
  2940.             if (PyList_Append(self->pers_func, pid) < 0) {
  2941.                 Py_DECREF(pid);
  2942.                 return -1;
  2943.             }
  2944.           }
  2945.         else {
  2946.             ARG_TUP(self, pid);
  2947.             if (self->arg) {
  2948.                 pid = PyObject_CallObject(self->pers_func, self->arg);
  2949.                 FREE_ARG_TUP(self);
  2950.             }
  2951.             if (! pid) return -1;
  2952.         }
  2953.  
  2954.         PDATA_PUSH(self->stack, pid, -1);
  2955.         return 0;
  2956.     }
  2957.     else {
  2958.       PyErr_SetString(UnpicklingError,
  2959.                       "A load persistent id instruction was encountered,\n"
  2960.                       "but no persistent_load function was specified.");
  2961.       return -1;
  2962.     }
  2963. }
  2964.  
  2965.  
  2966. static int
  2967. load_pop(Unpicklerobject *self) {
  2968.     int len;
  2969.  
  2970.     UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
  2971.  
  2972.     if ((self->num_marks > 0) && 
  2973.         (self->marks[self->num_marks - 1] == len))
  2974.         self->num_marks--;
  2975.     else 
  2976.         Py_DECREF(self->stack->data[--(self->stack->length)]);
  2977.  
  2978.     return 0;
  2979. }
  2980.  
  2981.  
  2982. static int
  2983. load_pop_mark(Unpicklerobject *self) {
  2984.     int i;
  2985.  
  2986.     if ((i = marker(self)) < 0)
  2987.         return -1;
  2988.  
  2989.     Pdata_clear(self->stack, i);
  2990.  
  2991.     return 0;
  2992. }
  2993.  
  2994.  
  2995. static int
  2996. load_dup(Unpicklerobject *self) {
  2997.     PyObject *last;
  2998.     int len;
  2999.  
  3000.     if ((len = self->stack->length) <= 0) return stackUnderflow();
  3001.     last=self->stack->data[len-1];
  3002.     Py_INCREF(last);
  3003.     PDATA_PUSH(self->stack, last, -1);
  3004.     return 0;
  3005. }
  3006.  
  3007.  
  3008. static int
  3009. load_get(Unpicklerobject *self) {
  3010.     PyObject *py_str = 0, *value = 0;
  3011.     int len, res = -1;
  3012.     char *s;
  3013.  
  3014.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  3015.     if (len < 2) return bad_readline();
  3016.  
  3017.     UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
  3018.  
  3019.     value = PyDict_GetItem(self->memo, py_str);
  3020.     Py_DECREF(py_str);
  3021.     if (! value) {
  3022.         PyErr_SetObject(BadPickleGet, py_str);
  3023.         return -1;
  3024.       }
  3025.  
  3026.     PDATA_APPEND(self->stack, value, -1);
  3027.     return 0;
  3028. }
  3029.  
  3030.  
  3031. static int
  3032. load_binget(Unpicklerobject *self) {
  3033.     PyObject *py_key = 0, *value = 0;
  3034.     unsigned char key;
  3035.     int res = -1;
  3036.     char *s;
  3037.  
  3038.     if ((*self->read_func)(self, &s, 1) < 0) return -1;
  3039.  
  3040.     key = (unsigned char)s[0];
  3041.     UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
  3042.     
  3043.     value = PyDict_GetItem(self->memo, py_key);
  3044.     Py_DECREF(py_key);
  3045.     if (! value) {
  3046.         PyErr_SetObject(BadPickleGet, py_key);
  3047.         return -1;
  3048.       }
  3049.  
  3050.     PDATA_APPEND(self->stack, value, -1);
  3051.     return 0;
  3052. }
  3053.  
  3054.  
  3055. static int
  3056. load_long_binget(Unpicklerobject *self) {
  3057.     PyObject *py_key = 0, *value = 0;
  3058.     unsigned char c, *s;
  3059.     long key;
  3060.     int res = -1;
  3061.  
  3062.     if ((*self->read_func)(self, &s, 4) < 0) return -1;
  3063.  
  3064.     c = (unsigned char)s[0];
  3065.     key = (long)c;
  3066.     c = (unsigned char)s[1];
  3067.     key |= (long)c << 8;
  3068.     c = (unsigned char)s[2];
  3069.     key |= (long)c << 16;
  3070.     c = (unsigned char)s[3];
  3071.     key |= (long)c << 24;
  3072.  
  3073.     UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
  3074.     
  3075.     value = PyDict_GetItem(self->memo, py_key);
  3076.     Py_DECREF(py_key);
  3077.     if (! value) {
  3078.         PyErr_SetObject(BadPickleGet, py_key);
  3079.         return -1;
  3080.       }
  3081.  
  3082.     PDATA_APPEND(self->stack, value, -1);
  3083.     return 0;
  3084. }
  3085.  
  3086.  
  3087. static int
  3088. load_put(Unpicklerobject *self) {
  3089.     PyObject *py_str = 0, *value = 0;
  3090.     int len, l;
  3091.     char *s;
  3092.  
  3093.     if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
  3094.     if (l < 2) return bad_readline();
  3095.     UNLESS (len=self->stack->length) return stackUnderflow();
  3096.     UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
  3097.     value=self->stack->data[len-1];
  3098.     l=PyDict_SetItem(self->memo, py_str, value);
  3099.     Py_DECREF(py_str);
  3100.     return l;
  3101. }
  3102.  
  3103.  
  3104. static int
  3105. load_binput(Unpicklerobject *self) {
  3106.     PyObject *py_key = 0, *value = 0;
  3107.     unsigned char key, *s;
  3108.     int len;
  3109.  
  3110.     if ((*self->read_func)(self, &s, 1) < 0) return -1;
  3111.     UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
  3112.  
  3113.     key = (unsigned char)s[0];
  3114.  
  3115.     UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
  3116.     value=self->stack->data[len-1];
  3117.     len=PyDict_SetItem(self->memo, py_key, value);
  3118.     Py_DECREF(py_key);
  3119.     return len;
  3120. }
  3121.  
  3122.  
  3123. static int
  3124. load_long_binput(Unpicklerobject *self) {
  3125.     PyObject *py_key = 0, *value = 0;
  3126.     long key;
  3127.     unsigned char c, *s;
  3128.     int len, res = -1;
  3129.  
  3130.     if ((*self->read_func)(self, &s, 4) < 0) return -1;
  3131.     UNLESS (len=self->stack->length) return stackUnderflow();
  3132.  
  3133.     c = (unsigned char)s[0];
  3134.     key = (long)c;
  3135.     c = (unsigned char)s[1];
  3136.     key |= (long)c << 8;
  3137.     c = (unsigned char)s[2];
  3138.     key |= (long)c << 16;
  3139.     c = (unsigned char)s[3];
  3140.     key |= (long)c << 24;
  3141.  
  3142.     UNLESS (py_key = PyInt_FromLong(key)) return -1;
  3143.     value=self->stack->data[len-1];
  3144.     len=PyDict_SetItem(self->memo, py_key, value);
  3145.     Py_DECREF(py_key);
  3146.     return len;
  3147. }
  3148.  
  3149.  
  3150. static int 
  3151. do_append(Unpicklerobject *self, int  x) {
  3152.     PyObject *value = 0, *list = 0, *append_method = 0;
  3153.     int len, i;
  3154.  
  3155.     UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
  3156.     if (len==x) return 0;       /* nothing to do */
  3157.  
  3158.     list=self->stack->data[x-1];
  3159.  
  3160.     if (PyList_Check(list)) {
  3161.         PyObject *slice;
  3162.         int list_len;
  3163.        
  3164.         slice=Pdata_popList(self->stack, x);
  3165.         list_len = PyList_GET_SIZE(list);
  3166.         i=PyList_SetSlice(list, list_len, list_len, slice);
  3167.         Py_DECREF(slice);
  3168.         return i;
  3169.     }
  3170.     else {
  3171.  
  3172.         UNLESS (append_method = PyObject_GetAttr(list, append_str))
  3173.             return -1;
  3174.          
  3175.         for (i = x; i < len; i++) {
  3176.             PyObject *junk;
  3177.  
  3178.             value=self->stack->data[i];
  3179.             junk=0;
  3180.             ARG_TUP(self, value);
  3181.             if (self->arg) {
  3182.               junk = PyObject_CallObject(append_method, self->arg);
  3183.               FREE_ARG_TUP(self);
  3184.             }
  3185.             if (! junk) {
  3186.                 Pdata_clear(self->stack, i+1);
  3187.                 self->stack->length=x;
  3188.                 Py_DECREF(append_method);
  3189.                 return -1;
  3190.             }
  3191.             Py_DECREF(junk);
  3192.         }
  3193.         self->stack->length=x;
  3194.         Py_DECREF(append_method);
  3195.     }
  3196.  
  3197.     return 0;
  3198. }
  3199.  
  3200.     
  3201. static int
  3202. load_append(Unpicklerobject *self) {
  3203.     return do_append(self, self->stack->length - 1);
  3204. }
  3205.  
  3206.  
  3207. static int
  3208. load_appends(Unpicklerobject *self) {
  3209.     return do_append(self, marker(self));
  3210. }
  3211.  
  3212.  
  3213. static int
  3214. do_setitems(Unpicklerobject *self, int  x) {
  3215.     PyObject *value = 0, *key = 0, *dict = 0;
  3216.     int len, i, r=0;
  3217.  
  3218.     UNLESS ((len=self->stack->length) >= x
  3219.             && x > 0) return stackUnderflow();
  3220.  
  3221.     dict=self->stack->data[x-1];
  3222.  
  3223.     for (i = x+1; i < len; i += 2) {
  3224.         key  =self->stack->data[i-1];
  3225.         value=self->stack->data[i  ];
  3226.         if (PyObject_SetItem(dict, key, value) < 0) {
  3227.             r=-1;
  3228.             break;
  3229.         }
  3230.     }
  3231.  
  3232.     Pdata_clear(self->stack, x);
  3233.  
  3234.     return r;
  3235. }
  3236.  
  3237.  
  3238. static int
  3239. load_setitem(Unpicklerobject *self) {
  3240.     return do_setitems(self, self->stack->length - 2);
  3241. }
  3242.  
  3243. static int
  3244. load_setitems(Unpicklerobject *self) {
  3245.     return do_setitems(self, marker(self));
  3246. }
  3247.  
  3248.  
  3249. static int
  3250. load_build(Unpicklerobject *self) {
  3251.     PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0, 
  3252.              *junk = 0, *__setstate__ = 0;
  3253.     int i, r = 0;
  3254.  
  3255.     if (self->stack->length < 2) return stackUnderflow();
  3256.     PDATA_POP(self->stack, value);
  3257.     if (! value) return -1;
  3258.     inst=self->stack->data[self->stack->length-1];
  3259.  
  3260.     if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
  3261.         ARG_TUP(self, value);
  3262.         if (self->arg) {
  3263.             junk = PyObject_CallObject(__setstate__, self->arg);
  3264.             FREE_ARG_TUP(self);
  3265.         }
  3266.         Py_DECREF(__setstate__);
  3267.         if (! junk) return -1;
  3268.         Py_DECREF(junk);
  3269.         return 0;
  3270.     }
  3271.  
  3272.     PyErr_Clear();
  3273.     if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
  3274.         i = 0;
  3275.         while (PyDict_Next(value, &i, &d_key, &d_value)) {
  3276.             if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
  3277.                 r=-1;
  3278.                 break;
  3279.             }
  3280.         }
  3281.         Py_DECREF(instdict);
  3282.     }
  3283.     else r=-1;
  3284.  
  3285.     Py_XDECREF(value);
  3286.   
  3287.     return r;
  3288. }
  3289.  
  3290.  
  3291. static int
  3292. load_mark(Unpicklerobject *self) {
  3293.     int s;
  3294.  
  3295.     if ((self->num_marks + 1) >= self->marks_size) {
  3296.         s=self->marks_size+20;
  3297.         if (s <= self->num_marks) s=self->num_marks + 1;
  3298.         if (self->marks == NULL)
  3299.             self->marks=(int *)malloc(s * sizeof(int));
  3300.         else
  3301.             self->marks=(int *)realloc(self->marks, s * sizeof(int));
  3302.         if (! self->marks) {
  3303.             PyErr_NoMemory();
  3304.             return -1;
  3305.         }
  3306.         self->marks_size = s;
  3307.     }
  3308.  
  3309.     self->marks[self->num_marks++] = self->stack->length;
  3310.  
  3311.     return 0;
  3312. }
  3313.  
  3314. static int
  3315. load_reduce(Unpicklerobject *self) {
  3316.     PyObject *callable = 0, *arg_tup = 0, *ob = 0;
  3317.  
  3318.     PDATA_POP(self->stack, arg_tup);
  3319.     if (! arg_tup) return -1;
  3320.     PDATA_POP(self->stack, callable);
  3321.     if (callable) {
  3322.         ob = Instance_New(callable, arg_tup);
  3323.         Py_DECREF(callable);
  3324.     }
  3325.     Py_DECREF(arg_tup);
  3326.  
  3327.     if (! ob) return -1;
  3328.    
  3329.     PDATA_PUSH(self->stack, ob, -1);
  3330.     return 0;
  3331. }
  3332.     
  3333. static PyObject *
  3334. load(Unpicklerobject *self) {
  3335.     PyObject *stack = 0, *err = 0, *val = 0;
  3336.     char *s;
  3337.  
  3338.     self->num_marks = 0;
  3339.     if (self->stack->length) Pdata_clear(self->stack, 0);
  3340.  
  3341.     while (1) {
  3342.         if ((*self->read_func)(self, &s, 1) < 0)
  3343.             break;
  3344.  
  3345.         switch (s[0]) {
  3346.             case NONE:
  3347.                 if (load_none(self) < 0)
  3348.                     break;
  3349.                 continue;
  3350.  
  3351.             case BININT:
  3352.                  if (load_binint(self) < 0)
  3353.                      break;
  3354.                  continue;
  3355.  
  3356.             case BININT1:
  3357.                 if (load_binint1(self) < 0)
  3358.                     break;
  3359.                 continue;
  3360.  
  3361.             case BININT2:
  3362.                 if (load_binint2(self) < 0)
  3363.                     break;
  3364.                 continue;
  3365.  
  3366.             case INT:
  3367.                 if (load_int(self) < 0)
  3368.                     break;
  3369.                 continue;
  3370.  
  3371.             case LONG:
  3372.                 if (load_long(self) < 0)
  3373.                     break;
  3374.                 continue;
  3375.  
  3376.             case FLOAT:
  3377.                 if (load_float(self) < 0)
  3378.                     break;
  3379.                 continue;
  3380.  
  3381.             case BINFLOAT:
  3382.                 if (load_binfloat(self) < 0)
  3383.                     break;
  3384.                 continue;
  3385.  
  3386.             case BINSTRING:
  3387.                 if (load_binstring(self) < 0)
  3388.                     break;
  3389.                 continue;
  3390.  
  3391.             case SHORT_BINSTRING:
  3392.                 if (load_short_binstring(self) < 0)
  3393.                     break;
  3394.                 continue;
  3395.  
  3396.             case STRING:
  3397.                 if (load_string(self) < 0)
  3398.                     break;
  3399.                 continue;
  3400.  
  3401.             case EMPTY_TUPLE:
  3402.                 if (load_empty_tuple(self) < 0)
  3403.                     break;
  3404.                 continue;
  3405.  
  3406.             case TUPLE:
  3407.                 if (load_tuple(self) < 0)
  3408.                     break;
  3409.                 continue;
  3410.  
  3411.             case EMPTY_LIST:
  3412.                 if (load_empty_list(self) < 0)
  3413.                     break;
  3414.                 continue;
  3415.  
  3416.             case LIST:
  3417.                 if (load_list(self) < 0)
  3418.                     break;
  3419.                 continue;
  3420.  
  3421.             case EMPTY_DICT:
  3422.                 if (load_empty_dict(self) < 0)
  3423.                     break;
  3424.                 continue;
  3425.  
  3426.             case DICT:
  3427.                 if (load_dict(self) < 0)
  3428.                     break;
  3429.                 continue;
  3430.  
  3431.             case OBJ:
  3432.                 if (load_obj(self) < 0)
  3433.                     break;
  3434.                 continue;
  3435.  
  3436.             case INST:
  3437.                 if (load_inst(self) < 0)
  3438.                     break;
  3439.                 continue;
  3440.  
  3441.             case GLOBAL:
  3442.                 if (load_global(self) < 0)
  3443.                     break;
  3444.                 continue;
  3445.  
  3446.             case APPEND:
  3447.                 if (load_append(self) < 0)
  3448.                     break;
  3449.                 continue;
  3450.  
  3451.             case APPENDS:
  3452.                 if (load_appends(self) < 0)
  3453.                     break;
  3454.                 continue;
  3455.    
  3456.             case BUILD:
  3457.                 if (load_build(self) < 0)
  3458.                     break;
  3459.                 continue;
  3460.   
  3461.             case DUP:
  3462.                 if (load_dup(self) < 0)
  3463.                     break;
  3464.                 continue;
  3465.  
  3466.             case BINGET:
  3467.                 if (load_binget(self) < 0)
  3468.                     break;
  3469.                 continue;
  3470.  
  3471.             case LONG_BINGET:
  3472.                 if (load_long_binget(self) < 0)
  3473.                     break;
  3474.                 continue;
  3475.          
  3476.             case GET:
  3477.                 if (load_get(self) < 0)
  3478.                     break;
  3479.                 continue;
  3480.  
  3481.             case MARK:
  3482.                 if (load_mark(self) < 0)
  3483.                     break;
  3484.                 continue;
  3485.  
  3486.             case BINPUT:
  3487.                 if (load_binput(self) < 0)
  3488.                     break;
  3489.                 continue;
  3490.  
  3491.             case LONG_BINPUT:
  3492.                 if (load_long_binput(self) < 0)
  3493.                     break;
  3494.                 continue;
  3495.          
  3496.             case PUT:
  3497.                 if (load_put(self) < 0)
  3498.                     break;
  3499.                 continue;
  3500.  
  3501.             case POP:
  3502.                 if (load_pop(self) < 0)
  3503.                     break;
  3504.                 continue;
  3505.  
  3506.             case POP_MARK:
  3507.                 if (load_pop_mark(self) < 0)
  3508.                     break;
  3509.                 continue;
  3510.  
  3511.             case SETITEM:
  3512.                 if (load_setitem(self) < 0)
  3513.                     break;
  3514.                 continue;
  3515.  
  3516.             case SETITEMS:
  3517.                 if (load_setitems(self) < 0)
  3518.                     break;
  3519.                 continue;
  3520.  
  3521.             case STOP:
  3522.                 break;
  3523.  
  3524.             case PERSID:
  3525.                 if (load_persid(self) < 0)
  3526.                     break;
  3527.                 continue;
  3528.  
  3529.             case BINPERSID:
  3530.                 if (load_binpersid(self) < 0)
  3531.                     break;
  3532.                 continue;
  3533.  
  3534.             case REDUCE:
  3535.                 if (load_reduce(self) < 0)
  3536.                     break;
  3537.                 continue;
  3538.  
  3539.             default: 
  3540.                 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.", 
  3541.                     "c", s[0]);
  3542.                 return NULL;
  3543.         }
  3544.  
  3545.         break;
  3546.     }
  3547.  
  3548.     if ((err = PyErr_Occurred())) {
  3549.         if (err == PyExc_EOFError) {
  3550.             PyErr_SetNone(PyExc_EOFError);
  3551.         }    
  3552.         return NULL;
  3553.     }
  3554.  
  3555.     PDATA_POP(self->stack, val);    
  3556.     return val;
  3557. }
  3558.     
  3559.  
  3560. /* No-load functions to support noload, which is used to
  3561.    find persistent references. */
  3562.  
  3563. static int
  3564. noload_obj(Unpicklerobject *self) {
  3565.     int i;
  3566.  
  3567.     if ((i = marker(self)) < 0) return -1;
  3568.     return Pdata_clear(self->stack, i+1);
  3569. }
  3570.  
  3571.  
  3572. static int
  3573. noload_inst(Unpicklerobject *self) {
  3574.     int i;
  3575.     char *s;
  3576.  
  3577.     if ((i = marker(self)) < 0) return -1;
  3578.     Pdata_clear(self->stack, i);
  3579.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3580.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3581.     PDATA_APPEND(self->stack, Py_None,-1);
  3582.     return 0;
  3583. }
  3584.  
  3585. static int
  3586. noload_global(Unpicklerobject *self) {
  3587.     char *s;
  3588.  
  3589.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3590.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3591.     PDATA_APPEND(self->stack, Py_None,-1);
  3592.     return 0;
  3593. }
  3594.  
  3595. static int
  3596. noload_reduce(Unpicklerobject *self) {
  3597.  
  3598.     if (self->stack->length < 2) return stackUnderflow();
  3599.     Pdata_clear(self->stack, self->stack->length-2);
  3600.     PDATA_APPEND(self->stack, Py_None,-1);
  3601.     return 0;
  3602. }
  3603.  
  3604. static int
  3605. noload_build(Unpicklerobject *self) {
  3606.  
  3607.   if (self->stack->length < 1) return stackUnderflow();
  3608.   Pdata_clear(self->stack, self->stack->length-1);
  3609.   return 0;
  3610. }
  3611.  
  3612.  
  3613. static PyObject *
  3614. noload(Unpicklerobject *self) {
  3615.     PyObject *stack = 0, *err = 0, *val = 0;
  3616.     char *s;
  3617.  
  3618.     self->num_marks = 0;
  3619.     Pdata_clear(self->stack, 0);
  3620.  
  3621.     while (1) {
  3622.         if ((*self->read_func)(self, &s, 1) < 0)
  3623.             break;
  3624.  
  3625.         switch (s[0]) {
  3626.             case NONE:
  3627.                 if (load_none(self) < 0)
  3628.                     break;
  3629.                 continue;
  3630.  
  3631.             case BININT:
  3632.                  if (load_binint(self) < 0)
  3633.                      break;
  3634.                  continue;
  3635.  
  3636.             case BININT1:
  3637.                 if (load_binint1(self) < 0)
  3638.                     break;
  3639.                 continue;
  3640.  
  3641.             case BININT2:
  3642.                 if (load_binint2(self) < 0)
  3643.                     break;
  3644.                 continue;
  3645.  
  3646.             case INT:
  3647.                 if (load_int(self) < 0)
  3648.                     break;
  3649.                 continue;
  3650.  
  3651.             case LONG:
  3652.                 if (load_long(self) < 0)
  3653.                     break;
  3654.                 continue;
  3655.  
  3656.             case FLOAT:
  3657.                 if (load_float(self) < 0)
  3658.                     break;
  3659.                 continue;
  3660.  
  3661.             case BINFLOAT:
  3662.                 if (load_binfloat(self) < 0)
  3663.                     break;
  3664.                 continue;
  3665.  
  3666.             case BINSTRING:
  3667.                 if (load_binstring(self) < 0)
  3668.                     break;
  3669.                 continue;
  3670.  
  3671.             case SHORT_BINSTRING:
  3672.                 if (load_short_binstring(self) < 0)
  3673.                     break;
  3674.                 continue;
  3675.  
  3676.             case STRING:
  3677.                 if (load_string(self) < 0)
  3678.                     break;
  3679.                 continue;
  3680.  
  3681.             case EMPTY_TUPLE:
  3682.                 if (load_empty_tuple(self) < 0)
  3683.                     break;
  3684.                 continue;
  3685.  
  3686.             case TUPLE:
  3687.                 if (load_tuple(self) < 0)
  3688.                     break;
  3689.                 continue;
  3690.  
  3691.             case EMPTY_LIST:
  3692.                 if (load_empty_list(self) < 0)
  3693.                     break;
  3694.                 continue;
  3695.  
  3696.             case LIST:
  3697.                 if (load_list(self) < 0)
  3698.                     break;
  3699.                 continue;
  3700.  
  3701.             case EMPTY_DICT:
  3702.                 if (load_empty_dict(self) < 0)
  3703.                     break;
  3704.                 continue;
  3705.  
  3706.             case DICT:
  3707.                 if (load_dict(self) < 0)
  3708.                     break;
  3709.                 continue;
  3710.  
  3711.             case OBJ:
  3712.                 if (noload_obj(self) < 0)
  3713.                     break;
  3714.                 continue;
  3715.  
  3716.             case INST:
  3717.                 if (noload_inst(self) < 0)
  3718.                     break;
  3719.                 continue;
  3720.  
  3721.             case GLOBAL:
  3722.                 if (noload_global(self) < 0)
  3723.                     break;
  3724.                 continue;
  3725.  
  3726.             case APPEND:
  3727.                 if (load_append(self) < 0)
  3728.                     break;
  3729.                 continue;
  3730.  
  3731.             case APPENDS:
  3732.                 if (load_appends(self) < 0)
  3733.                     break;
  3734.                 continue;
  3735.    
  3736.             case BUILD:
  3737.                 if (noload_build(self) < 0)
  3738.                     break;
  3739.                 continue;
  3740.   
  3741.             case DUP:
  3742.                 if (load_dup(self) < 0)
  3743.                     break;
  3744.                 continue;
  3745.  
  3746.             case BINGET:
  3747.                 if (load_binget(self) < 0)
  3748.                     break;
  3749.                 continue;
  3750.  
  3751.             case LONG_BINGET:
  3752.                 if (load_long_binget(self) < 0)
  3753.                     break;
  3754.                 continue;
  3755.          
  3756.             case GET:
  3757.                 if (load_get(self) < 0)
  3758.                     break;
  3759.                 continue;
  3760.  
  3761.             case MARK:
  3762.                 if (load_mark(self) < 0)
  3763.                     break;
  3764.                 continue;
  3765.  
  3766.             case BINPUT:
  3767.                 if (load_binput(self) < 0)
  3768.                     break;
  3769.                 continue;
  3770.  
  3771.             case LONG_BINPUT:
  3772.                 if (load_long_binput(self) < 0)
  3773.                     break;
  3774.                 continue;
  3775.          
  3776.             case PUT:
  3777.                 if (load_put(self) < 0)
  3778.                     break;
  3779.                 continue;
  3780.  
  3781.             case POP:
  3782.                 if (load_pop(self) < 0)
  3783.                     break;
  3784.                 continue;
  3785.  
  3786.             case POP_MARK:
  3787.                 if (load_pop_mark(self) < 0)
  3788.                     break;
  3789.                 continue;
  3790.  
  3791.             case SETITEM:
  3792.                 if (load_setitem(self) < 0)
  3793.                     break;
  3794.                 continue;
  3795.  
  3796.             case SETITEMS:
  3797.                 if (load_setitems(self) < 0)
  3798.                     break;
  3799.                 continue;
  3800.  
  3801.             case STOP:
  3802.                 break;
  3803.  
  3804.             case PERSID:
  3805.                 if (load_persid(self) < 0)
  3806.                     break;
  3807.                 continue;
  3808.  
  3809.             case BINPERSID:
  3810.                 if (load_binpersid(self) < 0)
  3811.                     break;
  3812.                 continue;
  3813.  
  3814.             case REDUCE:
  3815.                 if (noload_reduce(self) < 0)
  3816.                     break;
  3817.                 continue;
  3818.  
  3819.             default: 
  3820.                 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.", 
  3821.                     "c", s[0]);
  3822.                 return NULL;
  3823.         }
  3824.  
  3825.         break;
  3826.     }
  3827.  
  3828.     if ((err = PyErr_Occurred())) {
  3829.         if (err == PyExc_EOFError) {
  3830.             PyErr_SetNone(PyExc_EOFError);
  3831.         }    
  3832.         return NULL;
  3833.     }
  3834.  
  3835.     PDATA_POP(self->stack, val);    
  3836.     return val;
  3837. }
  3838.     
  3839.  
  3840. static PyObject *
  3841. Unpickler_load(Unpicklerobject *self, PyObject *args) {
  3842.     UNLESS (PyArg_ParseTuple(args, "")) 
  3843.         return NULL;
  3844.  
  3845.     return load(self);
  3846. }
  3847.  
  3848. static PyObject *
  3849. Unpickler_noload(Unpicklerobject *self, PyObject *args) {
  3850.     UNLESS (PyArg_ParseTuple(args, "")) 
  3851.         return NULL;
  3852.  
  3853.     return noload(self);
  3854. }
  3855.  
  3856.  
  3857. static struct PyMethodDef Unpickler_methods[] = {
  3858.   {"load",         (PyCFunction)Unpickler_load,   1,
  3859.    "load() -- Load a pickle"
  3860.   },
  3861.   {"noload",         (PyCFunction)Unpickler_noload,   1,
  3862.    "noload() -- not load a pickle, but go through most of the motions\n"
  3863.    "\n"
  3864.    "This function can be used to read past a pickle without instantiating\n"
  3865.    "any objects or importing any modules.  It can also be used to find all\n"
  3866.    "persistent references without instantiating any objects or importing\n"
  3867.    "any modules.\n"
  3868.   },
  3869.   {NULL,              NULL}           /* sentinel */
  3870. };
  3871.  
  3872.  
  3873. static Unpicklerobject *
  3874. newUnpicklerobject(PyObject *f) {
  3875.     Unpicklerobject *self;
  3876.  
  3877.     UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
  3878.         return NULL;
  3879.  
  3880.     self->file = NULL;
  3881.     self->arg = NULL;
  3882.     self->stack = (Pdata*)Pdata_New();
  3883.     self->pers_func = NULL;
  3884.     self->last_string = NULL;
  3885.     self->marks = NULL;
  3886.     self->num_marks = 0;
  3887.     self->marks_size = 0;
  3888.     self->buf_size = 0;
  3889.     self->read = NULL;
  3890.     self->readline = NULL;
  3891.     self->safe_constructors = NULL;
  3892.  
  3893.     UNLESS (self->memo = PyDict_New()) {
  3894.        Py_XDECREF((PyObject *)self);
  3895.        return NULL;
  3896.     }
  3897.  
  3898.     Py_INCREF(f);
  3899.     self->file = f;
  3900.  
  3901.     /* Set read, readline based on type of f */
  3902.     if (PyFile_Check(f)) {
  3903.         self->fp = PyFile_AsFile(f);
  3904.     if (self->fp == NULL) {
  3905.         PyErr_SetString(PyExc_IOError, "input file closed");
  3906.         return NULL;
  3907.     }
  3908.         self->read_func = read_file;
  3909.         self->readline_func = readline_file;
  3910.     }
  3911.     else if (PycStringIO_InputCheck(f)) {
  3912.         self->fp = NULL;
  3913.         self->read_func = read_cStringIO;
  3914.         self->readline_func = readline_cStringIO;
  3915.     }
  3916.     else {
  3917.  
  3918.         self->fp = NULL;
  3919.         self->read_func = read_other;
  3920.         self->readline_func = readline_other;
  3921.  
  3922.         UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
  3923.             (self->read = PyObject_GetAttr(f, read_str))) {
  3924.             PyErr_Clear();
  3925.             PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
  3926.                 "'readline' attributes" );
  3927.             goto err;
  3928.         }
  3929.     }
  3930.  
  3931.     if (PyEval_GetRestricted()) {
  3932.         /* Restricted execution, get private tables */
  3933.         PyObject *m;
  3934.  
  3935.         UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
  3936.         self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
  3937.         Py_DECREF(m);
  3938.         UNLESS (self->safe_constructors) goto err;
  3939.     }
  3940.     else {
  3941.         self->safe_constructors=safe_constructors;
  3942.         Py_INCREF(safe_constructors);
  3943.     }
  3944.  
  3945.     return self;
  3946.  
  3947. err:
  3948.     Py_DECREF((PyObject *)self);
  3949.     return NULL;
  3950. }
  3951.  
  3952.  
  3953. static PyObject *
  3954. get_Unpickler(PyObject *self, PyObject *args) {
  3955.     PyObject *file;
  3956.   
  3957.     UNLESS (PyArg_ParseTuple(args, "O", &file))
  3958.         return NULL;
  3959.     return (PyObject *)newUnpicklerobject(file);
  3960. }
  3961.  
  3962.  
  3963. static void
  3964. Unpickler_dealloc(Unpicklerobject *self) {
  3965.     Py_XDECREF(self->readline);
  3966.     Py_XDECREF(self->read);
  3967.     Py_XDECREF(self->file);
  3968.     Py_XDECREF(self->memo);
  3969.     Py_XDECREF(self->stack);
  3970.     Py_XDECREF(self->pers_func);
  3971.     Py_XDECREF(self->arg);
  3972.     Py_XDECREF(self->last_string);
  3973.     Py_XDECREF(self->safe_constructors);
  3974.  
  3975.     if (self->marks) {
  3976.         free(self->marks);
  3977.     }
  3978.  
  3979.     if (self->buf_size) {
  3980.         free(self->buf);
  3981.     }
  3982.     
  3983.     PyMem_DEL(self);
  3984. }
  3985.  
  3986.  
  3987. static PyObject *
  3988. Unpickler_getattr(Unpicklerobject *self, char *name) {
  3989.     if (!strcmp(name, "persistent_load")) {
  3990.         if (!self->pers_func) {
  3991.             PyErr_SetString(PyExc_AttributeError, name);
  3992.             return NULL;
  3993.         }
  3994.  
  3995.         Py_INCREF(self->pers_func);
  3996.         return self->pers_func;
  3997.     }
  3998.  
  3999.     if (!strcmp(name, "memo")) {
  4000.         if (!self->memo) {
  4001.             PyErr_SetString(PyExc_AttributeError, name);
  4002.             return NULL;
  4003.         }
  4004.  
  4005.         Py_INCREF(self->memo);
  4006.         return self->memo;
  4007.     }
  4008.  
  4009.     if (!strcmp(name, "UnpicklingError")) {
  4010.         Py_INCREF(UnpicklingError);
  4011.         return UnpicklingError;
  4012.     }
  4013.  
  4014.     return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
  4015. }
  4016.  
  4017.  
  4018. static int
  4019. Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
  4020.  
  4021.     if (! value) {
  4022.         PyErr_SetString(PyExc_TypeError,
  4023.                         "attribute deletion is not supported");
  4024.         return -1;
  4025.     }
  4026.  
  4027.     if (!strcmp(name, "persistent_load")) {
  4028.         Py_XDECREF(self->pers_func);
  4029.         self->pers_func = value;
  4030.         Py_INCREF(value);
  4031.         return 0;
  4032.     }
  4033.  
  4034.     if (strcmp(name, "memo") == 0) {
  4035.         if (! PyDict_Check(value)) {
  4036.           PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
  4037.           return -1;
  4038.         }
  4039.         Py_XDECREF(self->memo);
  4040.         self->memo = value;
  4041.         Py_INCREF(value);
  4042.         return 0;
  4043.     }
  4044.  
  4045.     PyErr_SetString(PyExc_AttributeError, name);
  4046.     return -1;
  4047. }
  4048.  
  4049.  
  4050. static PyObject *
  4051. cpm_dump(PyObject *self, PyObject *args) {
  4052.     PyObject *ob, *file, *res = NULL;
  4053.     Picklerobject *pickler = 0;
  4054.     int bin = 0;
  4055.  
  4056.     UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
  4057.         goto finally;
  4058.  
  4059.     UNLESS (pickler = newPicklerobject(file, bin))
  4060.         goto finally;
  4061.  
  4062.     if (dump(pickler, ob) < 0)
  4063.         goto finally;
  4064.  
  4065.     Py_INCREF(Py_None);
  4066.     res = Py_None;
  4067.  
  4068. finally:
  4069.     Py_XDECREF(pickler);
  4070.  
  4071.     return res;
  4072. }
  4073.  
  4074.  
  4075. static PyObject *
  4076. cpm_dumps(PyObject *self, PyObject *args) {
  4077.     PyObject *ob, *file = 0, *res = NULL;
  4078.     Picklerobject *pickler = 0;
  4079.     int bin = 0;
  4080.  
  4081.     UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &bin))
  4082.         goto finally;
  4083.  
  4084.     UNLESS (file = PycStringIO->NewOutput(128))
  4085.         goto finally;
  4086.  
  4087.     UNLESS (pickler = newPicklerobject(file, bin))
  4088.         goto finally;
  4089.  
  4090.     if (dump(pickler, ob) < 0)
  4091.         goto finally;
  4092.  
  4093.     res = PycStringIO->cgetvalue(file);
  4094.  
  4095. finally:
  4096.     Py_XDECREF(pickler);
  4097.     Py_XDECREF(file);
  4098.  
  4099.     return res;
  4100. }  
  4101.   
  4102.  
  4103. static PyObject *
  4104. cpm_load(PyObject *self, PyObject *args) {
  4105.     Unpicklerobject *unpickler = 0;
  4106.     PyObject *ob, *res = NULL;
  4107.  
  4108.     UNLESS (PyArg_ParseTuple(args, "O", &ob))
  4109.         goto finally;
  4110.  
  4111.     UNLESS (unpickler = newUnpicklerobject(ob))
  4112.         goto finally;
  4113.  
  4114.     res = load(unpickler);
  4115.  
  4116. finally:
  4117.     Py_XDECREF(unpickler);
  4118.  
  4119.     return res;
  4120. }
  4121.  
  4122.  
  4123. static PyObject *
  4124. cpm_loads(PyObject *self, PyObject *args) {
  4125.     PyObject *ob, *file = 0, *res = NULL;
  4126.     Unpicklerobject *unpickler = 0;
  4127.  
  4128.     UNLESS (PyArg_ParseTuple(args, "S", &ob))
  4129.         goto finally;
  4130.  
  4131.     UNLESS (file = PycStringIO->NewInput(ob))
  4132.         goto finally;
  4133.   
  4134.     UNLESS (unpickler = newUnpicklerobject(file))
  4135.         goto finally;
  4136.  
  4137.     res = load(unpickler);
  4138.  
  4139. finally:
  4140.     Py_XDECREF(file);
  4141.     Py_XDECREF(unpickler);
  4142.  
  4143.     return res;
  4144. }
  4145.  
  4146.  
  4147. static char Unpicklertype__doc__[] = 
  4148. "Objects that know how to unpickle";
  4149.  
  4150. static PyTypeObject Unpicklertype = {
  4151.     PyObject_HEAD_INIT(NULL)
  4152.     0,                            /*ob_size*/
  4153.     "Unpickler",                  /*tp_name*/
  4154.     sizeof(Unpicklerobject),              /*tp_basicsize*/
  4155.     0,                            /*tp_itemsize*/
  4156.     /* methods */
  4157.     (destructor)Unpickler_dealloc,        /*tp_dealloc*/
  4158.     (printfunc)0,         /*tp_print*/
  4159.     (getattrfunc)Unpickler_getattr,       /*tp_getattr*/
  4160.     (setattrfunc)Unpickler_setattr,       /*tp_setattr*/
  4161.     (cmpfunc)0,           /*tp_compare*/
  4162.     (reprfunc)0,          /*tp_repr*/
  4163.     0,                    /*tp_as_number*/
  4164.     0,            /*tp_as_sequence*/
  4165.     0,            /*tp_as_mapping*/
  4166.     (hashfunc)0,          /*tp_hash*/
  4167.     (ternaryfunc)0,               /*tp_call*/
  4168.     (reprfunc)0,          /*tp_str*/
  4169.  
  4170.     /* Space for future expansion */
  4171.     0L,0L,0L,0L,
  4172.     Unpicklertype__doc__ /* Documentation string */
  4173. };
  4174.  
  4175. static struct PyMethodDef cPickle_methods[] = {
  4176.   {"dump",         (PyCFunction)cpm_dump,         1,
  4177.    "dump(object, file, [binary]) --"
  4178.    "Write an object in pickle format to the given file\n"
  4179.    "\n"
  4180.    "If the optional argument, binary, is provided and is true, then the\n"
  4181.    "pickle will be written in binary format, which is more space and\n"
  4182.    "computationally efficient. \n"
  4183.   },
  4184.   {"dumps",        (PyCFunction)cpm_dumps,        1,
  4185.    "dumps(object, [binary]) --"
  4186.    "Return a string containing an object in pickle format\n"
  4187.    "\n"
  4188.    "If the optional argument, binary, is provided and is true, then the\n"
  4189.    "pickle will be written in binary format, which is more space and\n"
  4190.    "computationally efficient. \n"
  4191.   },
  4192.   {"load",         (PyCFunction)cpm_load,         1,
  4193.    "load(file) -- Load a pickle from the given file"},
  4194.   {"loads",        (PyCFunction)cpm_loads,        1,
  4195.    "loads(string) -- Load a pickle from the given string"},
  4196.   {"Pickler",      (PyCFunction)get_Pickler,      1,
  4197.    "Pickler(file, [binary]) -- Create a pickler\n"
  4198.    "\n"
  4199.    "If the optional argument, binary, is provided and is true, then\n"
  4200.    "pickles will be written in binary format, which is more space and\n"
  4201.    "computationally efficient. \n"
  4202.   },
  4203.   {"Unpickler",    (PyCFunction)get_Unpickler,    1,
  4204.    "Unpickler(file) -- Create an unpickler"},
  4205.   { NULL, NULL }
  4206. };
  4207.  
  4208.  
  4209. #define CHECK_FOR_ERRORS(MESS) \
  4210. if (PyErr_Occurred()) { \
  4211.     PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
  4212.     PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
  4213.     fprintf(stderr, # MESS ":\n\t"); \
  4214.     PyObject_Print(__sys_exc_type, stderr,0); \
  4215.     fprintf(stderr,", "); \
  4216.     PyObject_Print(__sys_exc_value, stderr,0); \
  4217.     fprintf(stderr,"\n"); \
  4218.     fflush(stderr); \
  4219.     Py_FatalError(# MESS); \
  4220. }
  4221.  
  4222.  
  4223. static int
  4224. init_stuff(PyObject *module, PyObject *module_dict) {
  4225.     PyObject *string, *copy_reg;
  4226.  
  4227. #define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
  4228.  
  4229.     INIT_STR(__class__);
  4230.     INIT_STR(__getinitargs__);
  4231.     INIT_STR(__dict__);
  4232.     INIT_STR(__getstate__);
  4233.     INIT_STR(__setstate__);
  4234.     INIT_STR(__name__);
  4235.     INIT_STR(__main__);
  4236.     INIT_STR(__reduce__);
  4237.     INIT_STR(write);
  4238.     INIT_STR(__safe_for_unpickling__);
  4239.     INIT_STR(append);
  4240.     INIT_STR(read);
  4241.     INIT_STR(readline);
  4242.     INIT_STR(copy_reg);
  4243.     INIT_STR(dispatch_table);
  4244.     INIT_STR(safe_constructors);
  4245.     INIT_STR(__basicnew__);
  4246.     UNLESS (empty_str=PyString_FromString("")) return -1;
  4247.  
  4248.     UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
  4249.         return -1;
  4250.  
  4251.     /* These next few are special because we want to use different
  4252.        ones in restricted mode. */
  4253.  
  4254.     UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
  4255.         return -1;
  4256.  
  4257.     UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
  4258.                                                 safe_constructors_str))
  4259.         return -1;
  4260.  
  4261.     Py_DECREF(copy_reg);
  4262.  
  4263.     /* Down to here ********************************** */
  4264.  
  4265.     UNLESS (string = PyImport_ImportModule("string"))
  4266.         return -1;
  4267.  
  4268.     UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
  4269.         return -1;
  4270.  
  4271.     Py_DECREF(string);
  4272.  
  4273.     UNLESS (empty_tuple = PyTuple_New(0))
  4274.         return -1;
  4275.  
  4276.     UNLESS (PicklingError = PyString_FromString("cPickle.PicklingError"))
  4277.         return -1;
  4278.  
  4279.     if (PyDict_SetItemString(module_dict, "PicklingError", 
  4280.         PicklingError) < 0)
  4281.         return -1;
  4282.  
  4283.     UNLESS (UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
  4284.         return -1;
  4285.  
  4286.     if (PyDict_SetItemString(module_dict, "UnpicklingError",
  4287.         UnpicklingError) < 0)
  4288.         return -1;
  4289.  
  4290.     UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
  4291.         return -1;
  4292.  
  4293.     if (PyDict_SetItemString(module_dict, "BadPickleGet",
  4294.         BadPickleGet) < 0)
  4295.         return -1;
  4296.  
  4297.     PycString_IMPORT;
  4298.  
  4299.     return 0;
  4300. }
  4301.  
  4302. #ifndef DL_EXPORT    /* declarations for DLL import/export */
  4303. #define DL_EXPORT(RTYPE) RTYPE
  4304. #endif
  4305. DL_EXPORT(void)
  4306. initcPickle() {
  4307.     PyObject *m, *d, *v;
  4308.     char *rev="1.63";
  4309.     PyObject *format_version;
  4310.     PyObject *compatible_formats;
  4311.  
  4312.     Picklertype.ob_type = &PyType_Type;
  4313.     Unpicklertype.ob_type = &PyType_Type;
  4314.     PdataType.ob_type = &PyType_Type;
  4315.  
  4316.     /* Create the module and add the functions */
  4317.     m = Py_InitModule4("cPickle", cPickle_methods,
  4318.                      cPickle_module_documentation,
  4319.                      (PyObject*)NULL,PYTHON_API_VERSION);
  4320.  
  4321.     /* Add some symbolic constants to the module */
  4322.     d = PyModule_GetDict(m);
  4323.     PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
  4324.     Py_XDECREF(v);
  4325.  
  4326.     format_version = PyString_FromString("1.3");
  4327.     compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
  4328.  
  4329.     PyDict_SetItemString(d, "format_version", format_version);
  4330.     PyDict_SetItemString(d, "compatible_formats", compatible_formats);
  4331.     Py_XDECREF(format_version);
  4332.     Py_XDECREF(compatible_formats);
  4333.  
  4334.     init_stuff(m, d);
  4335.     CHECK_FOR_ERRORS("can't initialize module cPickle");
  4336. }
  4337.